acquire.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire.cc,v 1.50 2004/03/17 05:17:11 mdz Exp $
  4. /* ######################################################################
  5. Acquire - File Acquiration
  6. The core element for the schedual system is the concept of a named
  7. queue. Each queue is unique and each queue has a name derived from the
  8. URI. The degree of paralization can be controled by how the queue
  9. name is derived from the URI.
  10. ##################################################################### */
  11. /*}}}*/
  12. // Include Files /*{{{*/
  13. #ifdef __GNUG__
  14. #pragma implementation "apt-pkg/acquire.h"
  15. #endif
  16. #include <apt-pkg/acquire.h>
  17. #include <apt-pkg/acquire-item.h>
  18. #include <apt-pkg/acquire-worker.h>
  19. #include <apt-pkg/configuration.h>
  20. #include <apt-pkg/error.h>
  21. #include <apt-pkg/strutl.h>
  22. #include <apti18n.h>
  23. #include <iostream>
  24. #include <dirent.h>
  25. #include <sys/time.h>
  26. #include <errno.h>
  27. #include <sys/stat.h>
  28. /*}}}*/
  29. using namespace std;
  30. // Acquire::pkgAcquire - Constructor /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* We grab some runtime state from the configuration space */
  33. pkgAcquire::pkgAcquire(pkgAcquireStatus *Log) : Log(Log)
  34. {
  35. Queues = 0;
  36. Configs = 0;
  37. Workers = 0;
  38. ToFetch = 0;
  39. Running = false;
  40. string Mode = _config->Find("Acquire::Queue-Mode","host");
  41. if (strcasecmp(Mode.c_str(),"host") == 0)
  42. QueueMode = QueueHost;
  43. if (strcasecmp(Mode.c_str(),"access") == 0)
  44. QueueMode = QueueAccess;
  45. Debug = _config->FindB("Debug::pkgAcquire",false);
  46. // This is really a stupid place for this
  47. struct stat St;
  48. if (stat((_config->FindDir("Dir::State::lists") + "partial/").c_str(),&St) != 0 ||
  49. S_ISDIR(St.st_mode) == 0)
  50. _error->Error(_("Lists directory %spartial is missing."),
  51. _config->FindDir("Dir::State::lists").c_str());
  52. if (stat((_config->FindDir("Dir::Cache::Archives") + "partial/").c_str(),&St) != 0 ||
  53. S_ISDIR(St.st_mode) == 0)
  54. _error->Error(_("Archive directory %spartial is missing."),
  55. _config->FindDir("Dir::Cache::Archives").c_str());
  56. }
  57. /*}}}*/
  58. // Acquire::~pkgAcquire - Destructor /*{{{*/
  59. // ---------------------------------------------------------------------
  60. /* Free our memory, clean up the queues (destroy the workers) */
  61. pkgAcquire::~pkgAcquire()
  62. {
  63. Shutdown();
  64. while (Configs != 0)
  65. {
  66. MethodConfig *Jnk = Configs;
  67. Configs = Configs->Next;
  68. delete Jnk;
  69. }
  70. }
  71. /*}}}*/
  72. // Acquire::Shutdown - Clean out the acquire object /*{{{*/
  73. // ---------------------------------------------------------------------
  74. /* */
  75. void pkgAcquire::Shutdown()
  76. {
  77. while (Items.size() != 0)
  78. {
  79. if (Items[0]->Status == Item::StatFetching)
  80. Items[0]->Status = Item::StatError;
  81. delete Items[0];
  82. }
  83. while (Queues != 0)
  84. {
  85. Queue *Jnk = Queues;
  86. Queues = Queues->Next;
  87. delete Jnk;
  88. }
  89. }
  90. /*}}}*/
  91. // Acquire::Add - Add a new item /*{{{*/
  92. // ---------------------------------------------------------------------
  93. /* This puts an item on the acquire list. This list is mainly for tracking
  94. item status */
  95. void pkgAcquire::Add(Item *Itm)
  96. {
  97. Items.push_back(Itm);
  98. }
  99. /*}}}*/
  100. // Acquire::Remove - Remove a item /*{{{*/
  101. // ---------------------------------------------------------------------
  102. /* Remove an item from the acquire list. This is usually not used.. */
  103. void pkgAcquire::Remove(Item *Itm)
  104. {
  105. Dequeue(Itm);
  106. for (ItemIterator I = Items.begin(); I != Items.end();)
  107. {
  108. if (*I == Itm)
  109. {
  110. Items.erase(I);
  111. I = Items.begin();
  112. }
  113. else
  114. I++;
  115. }
  116. }
  117. /*}}}*/
  118. // Acquire::Add - Add a worker /*{{{*/
  119. // ---------------------------------------------------------------------
  120. /* A list of workers is kept so that the select loop can direct their FD
  121. usage. */
  122. void pkgAcquire::Add(Worker *Work)
  123. {
  124. Work->NextAcquire = Workers;
  125. Workers = Work;
  126. }
  127. /*}}}*/
  128. // Acquire::Remove - Remove a worker /*{{{*/
  129. // ---------------------------------------------------------------------
  130. /* A worker has died. This can not be done while the select loop is running
  131. as it would require that RunFds could handling a changing list state and
  132. it cant.. */
  133. void pkgAcquire::Remove(Worker *Work)
  134. {
  135. if (Running == true)
  136. abort();
  137. Worker **I = &Workers;
  138. for (; *I != 0;)
  139. {
  140. if (*I == Work)
  141. *I = (*I)->NextAcquire;
  142. else
  143. I = &(*I)->NextAcquire;
  144. }
  145. }
  146. /*}}}*/
  147. // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
  148. // ---------------------------------------------------------------------
  149. /* This is the entry point for an item. An item calls this function when
  150. it is constructed which creates a queue (based on the current queue
  151. mode) and puts the item in that queue. If the system is running then
  152. the queue might be started. */
  153. void pkgAcquire::Enqueue(ItemDesc &Item)
  154. {
  155. // Determine which queue to put the item in
  156. const MethodConfig *Config;
  157. string Name = QueueName(Item.URI,Config);
  158. if (Name.empty() == true)
  159. return;
  160. // Find the queue structure
  161. Queue *I = Queues;
  162. for (; I != 0 && I->Name != Name; I = I->Next);
  163. if (I == 0)
  164. {
  165. I = new Queue(Name,this);
  166. I->Next = Queues;
  167. Queues = I;
  168. if (Running == true)
  169. I->Startup();
  170. }
  171. // See if this is a local only URI
  172. if (Config->LocalOnly == true && Item.Owner->Complete == false)
  173. Item.Owner->Local = true;
  174. Item.Owner->Status = Item::StatIdle;
  175. // Queue it into the named queue
  176. I->Enqueue(Item);
  177. ToFetch++;
  178. // Some trace stuff
  179. if (Debug == true)
  180. {
  181. clog << "Fetching " << Item.URI << endl;
  182. clog << " to " << Item.Owner->DestFile << endl;
  183. clog << " Queue is: " << Name << endl;
  184. }
  185. }
  186. /*}}}*/
  187. // Acquire::Dequeue - Remove an item from all queues /*{{{*/
  188. // ---------------------------------------------------------------------
  189. /* This is called when an item is finished being fetched. It removes it
  190. from all the queues */
  191. void pkgAcquire::Dequeue(Item *Itm)
  192. {
  193. Queue *I = Queues;
  194. bool Res = false;
  195. for (; I != 0; I = I->Next)
  196. Res |= I->Dequeue(Itm);
  197. if (Debug == true)
  198. clog << "Dequeuing " << Itm->DestFile << endl;
  199. if (Res == true)
  200. ToFetch--;
  201. }
  202. /*}}}*/
  203. // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
  204. // ---------------------------------------------------------------------
  205. /* The string returned depends on the configuration settings and the
  206. method parameters. Given something like http://foo.org/bar it can
  207. return http://foo.org or http */
  208. string pkgAcquire::QueueName(string Uri,MethodConfig const *&Config)
  209. {
  210. URI U(Uri);
  211. Config = GetConfig(U.Access);
  212. if (Config == 0)
  213. return string();
  214. /* Single-Instance methods get exactly one queue per URI. This is
  215. also used for the Access queue method */
  216. if (Config->SingleInstance == true || QueueMode == QueueAccess)
  217. return U.Access;
  218. return U.Access + ':' + U.Host;
  219. }
  220. /*}}}*/
  221. // Acquire::GetConfig - Fetch the configuration information /*{{{*/
  222. // ---------------------------------------------------------------------
  223. /* This locates the configuration structure for an access method. If
  224. a config structure cannot be found a Worker will be created to
  225. retrieve it */
  226. pkgAcquire::MethodConfig *pkgAcquire::GetConfig(string Access)
  227. {
  228. // Search for an existing config
  229. MethodConfig *Conf;
  230. for (Conf = Configs; Conf != 0; Conf = Conf->Next)
  231. if (Conf->Access == Access)
  232. return Conf;
  233. // Create the new config class
  234. Conf = new MethodConfig;
  235. Conf->Access = Access;
  236. Conf->Next = Configs;
  237. Configs = Conf;
  238. // Create the worker to fetch the configuration
  239. Worker Work(Conf);
  240. if (Work.Start() == false)
  241. return 0;
  242. return Conf;
  243. }
  244. /*}}}*/
  245. // Acquire::SetFds - Deal with readable FDs /*{{{*/
  246. // ---------------------------------------------------------------------
  247. /* Collect FDs that have activity monitors into the fd sets */
  248. void pkgAcquire::SetFds(int &Fd,fd_set *RSet,fd_set *WSet)
  249. {
  250. for (Worker *I = Workers; I != 0; I = I->NextAcquire)
  251. {
  252. if (I->InReady == true && I->InFd >= 0)
  253. {
  254. if (Fd < I->InFd)
  255. Fd = I->InFd;
  256. FD_SET(I->InFd,RSet);
  257. }
  258. if (I->OutReady == true && I->OutFd >= 0)
  259. {
  260. if (Fd < I->OutFd)
  261. Fd = I->OutFd;
  262. FD_SET(I->OutFd,WSet);
  263. }
  264. }
  265. }
  266. /*}}}*/
  267. // Acquire::RunFds - Deal with active FDs /*{{{*/
  268. // ---------------------------------------------------------------------
  269. /* Dispatch active FDs over to the proper workers. It is very important
  270. that a worker never be erased while this is running! The queue class
  271. should never erase a worker except during shutdown processing. */
  272. void pkgAcquire::RunFds(fd_set *RSet,fd_set *WSet)
  273. {
  274. for (Worker *I = Workers; I != 0; I = I->NextAcquire)
  275. {
  276. if (I->InFd >= 0 && FD_ISSET(I->InFd,RSet) != 0)
  277. I->InFdReady();
  278. if (I->OutFd >= 0 && FD_ISSET(I->OutFd,WSet) != 0)
  279. I->OutFdReady();
  280. }
  281. }
  282. /*}}}*/
  283. // Acquire::Run - Run the fetch sequence /*{{{*/
  284. // ---------------------------------------------------------------------
  285. /* This runs the queues. It manages a select loop for all of the
  286. Worker tasks. The workers interact with the queues and items to
  287. manage the actual fetch. */
  288. pkgAcquire::RunResult pkgAcquire::Run()
  289. {
  290. Running = true;
  291. for (Queue *I = Queues; I != 0; I = I->Next)
  292. I->Startup();
  293. if (Log != 0)
  294. Log->Start();
  295. bool WasCancelled = false;
  296. // Run till all things have been acquired
  297. struct timeval tv;
  298. tv.tv_sec = 0;
  299. tv.tv_usec = 500000;
  300. while (ToFetch > 0)
  301. {
  302. fd_set RFds;
  303. fd_set WFds;
  304. int Highest = 0;
  305. FD_ZERO(&RFds);
  306. FD_ZERO(&WFds);
  307. SetFds(Highest,&RFds,&WFds);
  308. int Res;
  309. do
  310. {
  311. Res = select(Highest+1,&RFds,&WFds,0,&tv);
  312. }
  313. while (Res < 0 && errno == EINTR);
  314. if (Res < 0)
  315. {
  316. _error->Errno("select","Select has failed");
  317. break;
  318. }
  319. RunFds(&RFds,&WFds);
  320. if (_error->PendingError() == true)
  321. break;
  322. // Timeout, notify the log class
  323. if (Res == 0 || (Log != 0 && Log->Update == true))
  324. {
  325. tv.tv_usec = 500000;
  326. for (Worker *I = Workers; I != 0; I = I->NextAcquire)
  327. I->Pulse();
  328. if (Log != 0 && Log->Pulse(this) == false)
  329. {
  330. WasCancelled = true;
  331. break;
  332. }
  333. }
  334. }
  335. if (Log != 0)
  336. Log->Stop();
  337. // Shut down the acquire bits
  338. Running = false;
  339. for (Queue *I = Queues; I != 0; I = I->Next)
  340. I->Shutdown(false);
  341. // Shut down the items
  342. for (ItemIterator I = Items.begin(); I != Items.end(); I++)
  343. (*I)->Finished();
  344. if (_error->PendingError())
  345. return Failed;
  346. if (WasCancelled)
  347. return Cancelled;
  348. return Continue;
  349. }
  350. /*}}}*/
  351. // Acquire::Bump - Called when an item is dequeued /*{{{*/
  352. // ---------------------------------------------------------------------
  353. /* This routine bumps idle queues in hopes that they will be able to fetch
  354. the dequeued item */
  355. void pkgAcquire::Bump()
  356. {
  357. for (Queue *I = Queues; I != 0; I = I->Next)
  358. I->Bump();
  359. }
  360. /*}}}*/
  361. // Acquire::WorkerStep - Step to the next worker /*{{{*/
  362. // ---------------------------------------------------------------------
  363. /* Not inlined to advoid including acquire-worker.h */
  364. pkgAcquire::Worker *pkgAcquire::WorkerStep(Worker *I)
  365. {
  366. return I->NextAcquire;
  367. };
  368. /*}}}*/
  369. // Acquire::Clean - Cleans a directory /*{{{*/
  370. // ---------------------------------------------------------------------
  371. /* This is a bit simplistic, it looks at every file in the dir and sees
  372. if it is part of the download set. */
  373. bool pkgAcquire::Clean(string Dir)
  374. {
  375. DIR *D = opendir(Dir.c_str());
  376. if (D == 0)
  377. return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
  378. string StartDir = SafeGetCWD();
  379. if (chdir(Dir.c_str()) != 0)
  380. {
  381. closedir(D);
  382. return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
  383. }
  384. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  385. {
  386. // Skip some files..
  387. if (strcmp(Dir->d_name,"lock") == 0 ||
  388. strcmp(Dir->d_name,"partial") == 0 ||
  389. strcmp(Dir->d_name,".") == 0 ||
  390. strcmp(Dir->d_name,"..") == 0)
  391. continue;
  392. // Look in the get list
  393. ItemCIterator I = Items.begin();
  394. for (; I != Items.end(); I++)
  395. if (flNotDir((*I)->DestFile) == Dir->d_name)
  396. break;
  397. // Nothing found, nuke it
  398. if (I == Items.end())
  399. unlink(Dir->d_name);
  400. };
  401. chdir(StartDir.c_str());
  402. closedir(D);
  403. return true;
  404. }
  405. /*}}}*/
  406. // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
  407. // ---------------------------------------------------------------------
  408. /* This is the total number of bytes needed */
  409. double pkgAcquire::TotalNeeded()
  410. {
  411. double Total = 0;
  412. for (ItemCIterator I = ItemsBegin(); I != ItemsEnd(); I++)
  413. Total += (*I)->FileSize;
  414. return Total;
  415. }
  416. /*}}}*/
  417. // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
  418. // ---------------------------------------------------------------------
  419. /* This is the number of bytes that is not local */
  420. double pkgAcquire::FetchNeeded()
  421. {
  422. double Total = 0;
  423. for (ItemCIterator I = ItemsBegin(); I != ItemsEnd(); I++)
  424. if ((*I)->Local == false)
  425. Total += (*I)->FileSize;
  426. return Total;
  427. }
  428. /*}}}*/
  429. // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
  430. // ---------------------------------------------------------------------
  431. /* This is the number of bytes that is not local */
  432. double pkgAcquire::PartialPresent()
  433. {
  434. double Total = 0;
  435. for (ItemCIterator I = ItemsBegin(); I != ItemsEnd(); I++)
  436. if ((*I)->Local == false)
  437. Total += (*I)->PartialSize;
  438. return Total;
  439. }
  440. // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
  441. // ---------------------------------------------------------------------
  442. /* */
  443. pkgAcquire::UriIterator pkgAcquire::UriBegin()
  444. {
  445. return UriIterator(Queues);
  446. }
  447. /*}}}*/
  448. // Acquire::UriEnd - End iterator for the uri list /*{{{*/
  449. // ---------------------------------------------------------------------
  450. /* */
  451. pkgAcquire::UriIterator pkgAcquire::UriEnd()
  452. {
  453. return UriIterator(0);
  454. }
  455. /*}}}*/
  456. // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
  457. // ---------------------------------------------------------------------
  458. /* */
  459. pkgAcquire::MethodConfig::MethodConfig()
  460. {
  461. SingleInstance = false;
  462. Pipeline = false;
  463. SendConfig = false;
  464. LocalOnly = false;
  465. Removable = false;
  466. Next = 0;
  467. }
  468. /*}}}*/
  469. // Queue::Queue - Constructor /*{{{*/
  470. // ---------------------------------------------------------------------
  471. /* */
  472. pkgAcquire::Queue::Queue(string Name,pkgAcquire *Owner) : Name(Name),
  473. Owner(Owner)
  474. {
  475. Items = 0;
  476. Next = 0;
  477. Workers = 0;
  478. MaxPipeDepth = 1;
  479. PipeDepth = 0;
  480. }
  481. /*}}}*/
  482. // Queue::~Queue - Destructor /*{{{*/
  483. // ---------------------------------------------------------------------
  484. /* */
  485. pkgAcquire::Queue::~Queue()
  486. {
  487. Shutdown(true);
  488. while (Items != 0)
  489. {
  490. QItem *Jnk = Items;
  491. Items = Items->Next;
  492. delete Jnk;
  493. }
  494. }
  495. /*}}}*/
  496. // Queue::Enqueue - Queue an item to the queue /*{{{*/
  497. // ---------------------------------------------------------------------
  498. /* */
  499. void pkgAcquire::Queue::Enqueue(ItemDesc &Item)
  500. {
  501. QItem **I = &Items;
  502. for (; *I != 0; I = &(*I)->Next);
  503. // Create a new item
  504. QItem *Itm = new QItem;
  505. *Itm = Item;
  506. Itm->Next = 0;
  507. *I = Itm;
  508. Item.Owner->QueueCounter++;
  509. if (Items->Next == 0)
  510. Cycle();
  511. }
  512. /*}}}*/
  513. // Queue::Dequeue - Remove an item from the queue /*{{{*/
  514. // ---------------------------------------------------------------------
  515. /* We return true if we hit something */
  516. bool pkgAcquire::Queue::Dequeue(Item *Owner)
  517. {
  518. if (Owner->Status == pkgAcquire::Item::StatFetching)
  519. return _error->Error("Tried to dequeue a fetching object");
  520. bool Res = false;
  521. QItem **I = &Items;
  522. for (; *I != 0;)
  523. {
  524. if ((*I)->Owner == Owner)
  525. {
  526. QItem *Jnk= *I;
  527. *I = (*I)->Next;
  528. Owner->QueueCounter--;
  529. delete Jnk;
  530. Res = true;
  531. }
  532. else
  533. I = &(*I)->Next;
  534. }
  535. return Res;
  536. }
  537. /*}}}*/
  538. // Queue::Startup - Start the worker processes /*{{{*/
  539. // ---------------------------------------------------------------------
  540. /* It is possible for this to be called with a pre-existing set of
  541. workers. */
  542. bool pkgAcquire::Queue::Startup()
  543. {
  544. if (Workers == 0)
  545. {
  546. URI U(Name);
  547. pkgAcquire::MethodConfig *Cnf = Owner->GetConfig(U.Access);
  548. if (Cnf == 0)
  549. return false;
  550. Workers = new Worker(this,Cnf,Owner->Log);
  551. Owner->Add(Workers);
  552. if (Workers->Start() == false)
  553. return false;
  554. /* When pipelining we commit 10 items. This needs to change when we
  555. added other source retry to have cycle maintain a pipeline depth
  556. on its own. */
  557. if (Cnf->Pipeline == true)
  558. MaxPipeDepth = 10;
  559. else
  560. MaxPipeDepth = 1;
  561. }
  562. return Cycle();
  563. }
  564. /*}}}*/
  565. // Queue::Shutdown - Shutdown the worker processes /*{{{*/
  566. // ---------------------------------------------------------------------
  567. /* If final is true then all workers are eliminated, otherwise only workers
  568. that do not need cleanup are removed */
  569. bool pkgAcquire::Queue::Shutdown(bool Final)
  570. {
  571. // Delete all of the workers
  572. pkgAcquire::Worker **Cur = &Workers;
  573. while (*Cur != 0)
  574. {
  575. pkgAcquire::Worker *Jnk = *Cur;
  576. if (Final == true || Jnk->GetConf()->NeedsCleanup == false)
  577. {
  578. *Cur = Jnk->NextQueue;
  579. Owner->Remove(Jnk);
  580. delete Jnk;
  581. }
  582. else
  583. Cur = &(*Cur)->NextQueue;
  584. }
  585. return true;
  586. }
  587. /*}}}*/
  588. // Queue::FindItem - Find a URI in the item list /*{{{*/
  589. // ---------------------------------------------------------------------
  590. /* */
  591. pkgAcquire::Queue::QItem *pkgAcquire::Queue::FindItem(string URI,pkgAcquire::Worker *Owner)
  592. {
  593. for (QItem *I = Items; I != 0; I = I->Next)
  594. if (I->URI == URI && I->Worker == Owner)
  595. return I;
  596. return 0;
  597. }
  598. /*}}}*/
  599. // Queue::ItemDone - Item has been completed /*{{{*/
  600. // ---------------------------------------------------------------------
  601. /* The worker signals this which causes the item to be removed from the
  602. queue. If this is the last queue instance then it is removed from the
  603. main queue too.*/
  604. bool pkgAcquire::Queue::ItemDone(QItem *Itm)
  605. {
  606. PipeDepth--;
  607. if (Itm->Owner->Status == pkgAcquire::Item::StatFetching)
  608. Itm->Owner->Status = pkgAcquire::Item::StatDone;
  609. if (Itm->Owner->QueueCounter <= 1)
  610. Owner->Dequeue(Itm->Owner);
  611. else
  612. {
  613. Dequeue(Itm->Owner);
  614. Owner->Bump();
  615. }
  616. return Cycle();
  617. }
  618. /*}}}*/
  619. // Queue::Cycle - Queue new items into the method /*{{{*/
  620. // ---------------------------------------------------------------------
  621. /* This locates a new idle item and sends it to the worker. If pipelining
  622. is enabled then it keeps the pipe full. */
  623. bool pkgAcquire::Queue::Cycle()
  624. {
  625. if (Items == 0 || Workers == 0)
  626. return true;
  627. if (PipeDepth < 0)
  628. return _error->Error("Pipedepth failure");
  629. // Look for a queable item
  630. QItem *I = Items;
  631. while (PipeDepth < (signed)MaxPipeDepth)
  632. {
  633. for (; I != 0; I = I->Next)
  634. if (I->Owner->Status == pkgAcquire::Item::StatIdle)
  635. break;
  636. // Nothing to do, queue is idle.
  637. if (I == 0)
  638. return true;
  639. I->Worker = Workers;
  640. I->Owner->Status = pkgAcquire::Item::StatFetching;
  641. PipeDepth++;
  642. if (Workers->QueueItem(I) == false)
  643. return false;
  644. }
  645. return true;
  646. }
  647. /*}}}*/
  648. // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
  649. // ---------------------------------------------------------------------
  650. /* This is called when an item in multiple queues is dequeued */
  651. void pkgAcquire::Queue::Bump()
  652. {
  653. Cycle();
  654. }
  655. /*}}}*/
  656. // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
  657. // ---------------------------------------------------------------------
  658. /* */
  659. pkgAcquireStatus::pkgAcquireStatus() : Update(true), MorePulses(false)
  660. {
  661. Start();
  662. }
  663. /*}}}*/
  664. // AcquireStatus::Pulse - Called periodically /*{{{*/
  665. // ---------------------------------------------------------------------
  666. /* This computes some internal state variables for the derived classes to
  667. use. It generates the current downloaded bytes and total bytes to download
  668. as well as the current CPS estimate. */
  669. bool pkgAcquireStatus::Pulse(pkgAcquire *Owner)
  670. {
  671. TotalBytes = 0;
  672. CurrentBytes = 0;
  673. TotalItems = 0;
  674. CurrentItems = 0;
  675. // Compute the total number of bytes to fetch
  676. unsigned int Unknown = 0;
  677. unsigned int Count = 0;
  678. for (pkgAcquire::ItemCIterator I = Owner->ItemsBegin(); I != Owner->ItemsEnd();
  679. I++, Count++)
  680. {
  681. TotalItems++;
  682. if ((*I)->Status == pkgAcquire::Item::StatDone)
  683. CurrentItems++;
  684. // Totally ignore local items
  685. if ((*I)->Local == true)
  686. continue;
  687. TotalBytes += (*I)->FileSize;
  688. if ((*I)->Complete == true)
  689. CurrentBytes += (*I)->FileSize;
  690. if ((*I)->FileSize == 0 && (*I)->Complete == false)
  691. Unknown++;
  692. }
  693. // Compute the current completion
  694. unsigned long ResumeSize = 0;
  695. for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
  696. I = Owner->WorkerStep(I))
  697. if (I->CurrentItem != 0 && I->CurrentItem->Owner->Complete == false)
  698. {
  699. CurrentBytes += I->CurrentSize;
  700. ResumeSize += I->ResumePoint;
  701. // Files with unknown size always have 100% completion
  702. if (I->CurrentItem->Owner->FileSize == 0 &&
  703. I->CurrentItem->Owner->Complete == false)
  704. TotalBytes += I->CurrentSize;
  705. }
  706. // Normalize the figures and account for unknown size downloads
  707. if (TotalBytes <= 0)
  708. TotalBytes = 1;
  709. if (Unknown == Count)
  710. TotalBytes = Unknown;
  711. // Wha?! Is not supposed to happen.
  712. if (CurrentBytes > TotalBytes)
  713. CurrentBytes = TotalBytes;
  714. // Compute the CPS
  715. struct timeval NewTime;
  716. gettimeofday(&NewTime,0);
  717. if (NewTime.tv_sec - Time.tv_sec == 6 && NewTime.tv_usec > Time.tv_usec ||
  718. NewTime.tv_sec - Time.tv_sec > 6)
  719. {
  720. double Delta = NewTime.tv_sec - Time.tv_sec +
  721. (NewTime.tv_usec - Time.tv_usec)/1000000.0;
  722. // Compute the CPS value
  723. if (Delta < 0.01)
  724. CurrentCPS = 0;
  725. else
  726. CurrentCPS = ((CurrentBytes - ResumeSize) - LastBytes)/Delta;
  727. LastBytes = CurrentBytes - ResumeSize;
  728. ElapsedTime = (unsigned long)Delta;
  729. Time = NewTime;
  730. }
  731. return true;
  732. }
  733. /*}}}*/
  734. // AcquireStatus::Start - Called when the download is started /*{{{*/
  735. // ---------------------------------------------------------------------
  736. /* We just reset the counters */
  737. void pkgAcquireStatus::Start()
  738. {
  739. gettimeofday(&Time,0);
  740. gettimeofday(&StartTime,0);
  741. LastBytes = 0;
  742. CurrentCPS = 0;
  743. CurrentBytes = 0;
  744. TotalBytes = 0;
  745. FetchedBytes = 0;
  746. ElapsedTime = 0;
  747. TotalItems = 0;
  748. CurrentItems = 0;
  749. }
  750. /*}}}*/
  751. // AcquireStatus::Stop - Finished downloading /*{{{*/
  752. // ---------------------------------------------------------------------
  753. /* This accurately computes the elapsed time and the total overall CPS. */
  754. void pkgAcquireStatus::Stop()
  755. {
  756. // Compute the CPS and elapsed time
  757. struct timeval NewTime;
  758. gettimeofday(&NewTime,0);
  759. double Delta = NewTime.tv_sec - StartTime.tv_sec +
  760. (NewTime.tv_usec - StartTime.tv_usec)/1000000.0;
  761. // Compute the CPS value
  762. if (Delta < 0.01)
  763. CurrentCPS = 0;
  764. else
  765. CurrentCPS = FetchedBytes/Delta;
  766. LastBytes = CurrentBytes;
  767. ElapsedTime = (unsigned int)Delta;
  768. }
  769. /*}}}*/
  770. // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
  771. // ---------------------------------------------------------------------
  772. /* This is used to get accurate final transfer rate reporting. */
  773. void pkgAcquireStatus::Fetched(unsigned long Size,unsigned long Resume)
  774. {
  775. FetchedBytes += Size - Resume;
  776. }
  777. /*}}}*/