acquire.cc 24 KB

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