acquire.cc 22 KB

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