Changeset 14083


Ignore:
Timestamp:
May 27, 2013, 9:04:48 PM (9 years ago)
Author:
jordan
Message:

(trunk, libT) fix tr_torrentStat() regression in the nightlies reported in #5294 by mw3demo

Location:
trunk/libtransmission
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-mgr.c

    r14076 r14083  
    487487
    488488static void
     489resetTorrentStats (tr_torrent * tor)
     490{
     491  int i;
     492
     493  tor->peerCount = 0;
     494  tor->activePeerCount[TR_UP] = 0;
     495  tor->activePeerCount[TR_DOWN] = 0;
     496  tor->activeWebseedCount = 0;
     497  for (i=0; i<TR_PEER_FROM__MAX; i++)
     498    tor->peerFromCount[i] = 0;
     499}
     500
     501static void
    489502swarmFree (void * vs)
    490503{
     
    501514  tr_ptrArrayDestruct (&s->outgoingHandshakes, NULL);
    502515  tr_ptrArrayDestruct (&s->peers, NULL);
     516  resetTorrentStats (s->tor);
    503517
    504518  replicationFree (s);
     
    520534  tr_ptrArrayDestruct (&s->webseeds, (PtrArrayForeachFunc)tr_peerFree);
    521535  s->webseeds = TR_PTR_ARRAY_INIT;
     536  s->tor->activeWebseedCount = 0;
    522537
    523538  /* repopulate it */
     
    594609  managerUnlock (manager);
    595610  tr_free (manager);
    596 }
    597 
    598 static int
    599 clientIsDownloadingFrom (const tr_torrent * tor, const tr_peerMsgs * p)
    600 {
    601   if (!tr_torrentHasMetadata (tor))
    602     return true;
    603 
    604   return tr_peerMsgsIsClientInterested (p) && !tr_peerMsgsIsClientChoked (p);
    605 }
    606 
    607 static int
    608 clientIsUploadingTo (const tr_peerMsgs * p)
    609 {
    610   return tr_peerMsgsIsPeerInterested (p) && !tr_peerMsgsIsPeerChoked (p);
    611611}
    612612
     
    869869countActiveWebseeds (tr_swarm * s)
    870870{
    871   int i;
    872   int activeCount = 0;
    873   const int n = tr_ptrArraySize (&s->webseeds);
    874   const uint64_t now = tr_time_msec ();
    875 
    876   for (i=0; i<n; ++i)
    877     if (tr_peerIsTransferringPieces (tr_ptrArrayNth(&s->webseeds,i), now, TR_DOWN, NULL))
    878       ++activeCount;
     871  int activeCount;
     872
     873  if (s->tor->isRunning && !tr_torrentIsSeed (s->tor))
     874    {
     875      int i;
     876      const int n = tr_ptrArraySize (&s->webseeds);
     877      const uint64_t now = tr_time_msec ();
     878
     879      for (i=0; i<n; ++i)
     880        if (tr_peerIsTransferringPieces (tr_ptrArrayNth(&s->webseeds,i), now, TR_DOWN, NULL))
     881          ++activeCount;
     882    }
     883  else
     884    {
     885      activeCount = 0;
     886    }
    879887
    880888  return activeCount;
     
    19321940
    19331941  tr_ptrArrayInsertSorted (&swarm->peers, peer, peerCompare);
     1942  ++tor->peerCount;
     1943  ++tor->peerFromCount[atom->fromFirst];
     1944
     1945  assert (tor->peerCount == tr_ptrArraySize (&swarm->peers));
     1946  assert (tor->peerFromCount[atom->fromFirst] <= tor->peerCount);
    19341947}
    19351948
     
    24202433}
    24212434
     2435static void removeAllPeers (tr_swarm *);
     2436
    24222437static void
    24232438stopSwarm (tr_swarm * swarm)
    24242439{
    2425   tr_peer * peer;
    2426 
    24272440  swarm->isRunning = false;
    24282441
     
    24302443  invalidatePieceSorting (swarm);
    24312444
    2432   /* disconnect the peers. */
    2433   while ((peer = tr_ptrArrayPop (&swarm->peers)))
    2434     tr_peerFree (peer);
     2445  removeAllPeers (swarm);
    24352446
    24362447  /* disconnect the handshakes. handshakeAbort calls handshakeDoneCB (),
     
    25222533  for (i=0; i<peerCount; ++i)
    25232534    tr_peerUpdateProgress (tor, peers[i]);
     2535
     2536  /* update the bittorrent peers' willingnes... */
     2537  for (i=0; i<peerCount; ++i)
     2538    {
     2539      tr_peerMsgsUpdateActive (tr_peerMsgsCast(peers[i]), TR_UP);
     2540      tr_peerMsgsUpdateActive (tr_peerMsgsCast(peers[i]), TR_DOWN);
     2541    }
    25242542}
    25252543
     
    26162634  assert (desiredAvailable <= tor->info.totalSize);
    26172635  return desiredAvailable;
    2618 }
    2619 
    2620 void
    2621 tr_peerMgrTorrentStats (tr_torrent  * tor,
    2622                         int         * setmePeersConnected,
    2623                         int         * setmeWebseedsSendingToUs,
    2624                         int         * setmePeersSendingToUs,
    2625                         int         * setmePeersGettingFromUs,
    2626                         int         * setmePeersFrom)
    2627 {
    2628   int i;
    2629   int n;
    2630   tr_swarm * s;
    2631 
    2632   assert (tr_isTorrent (tor));
    2633 
    2634   *setmePeersConnected       = 0;
    2635   *setmePeersGettingFromUs   = 0;
    2636   *setmePeersSendingToUs     = 0;
    2637   *setmeWebseedsSendingToUs  = 0;
    2638 
    2639   s = tor->swarm;
    2640   n = tr_ptrArraySize (&s->peers);
    2641 
    2642   for (i=0; i<TR_PEER_FROM__MAX; ++i)
    2643     setmePeersFrom[i] = 0;
    2644 
    2645   for (i=0; i<n; ++i)
    2646     {
    2647       tr_peer * peer = tr_ptrArrayNth (&s->peers, i);
    2648       tr_peerMsgs * msgs = PEER_MSGS (peer);
    2649       const struct peer_atom * atom = peer->atom;
    2650 
    2651       assert (msgs != NULL);
    2652 
    2653       ++*setmePeersConnected;
    2654 
    2655       ++setmePeersFrom[atom->fromFirst];
    2656 
    2657       if (clientIsDownloadingFrom (tor, msgs))
    2658         ++*setmePeersSendingToUs;
    2659 
    2660       if (clientIsUploadingTo (msgs))
    2661         ++*setmePeersGettingFromUs;
    2662     }
    2663 
    2664   *setmeWebseedsSendingToUs = countActiveWebseeds (s);
    26652636}
    26662637
     
    27362707      stat->clientIsInterested  = tr_peerMsgsIsClientInterested (msgs);
    27372708      stat->isIncoming          = tr_peerMsgsIsIncomingConnection (msgs);
    2738       stat->isDownloadingFrom   = clientIsDownloadingFrom (tor, msgs);
    2739       stat->isUploadingTo       = clientIsUploadingTo (msgs);
     2709      stat->isDownloadingFrom   = tr_peerMsgsIsActive (msgs, TR_PEER_TO_CLIENT);
     2710      stat->isUploadingTo       = tr_peerMsgsIsActive (msgs, TR_CLIENT_TO_PEER);
    27402711      stat->isSeed              = peerIsSeed (peer);
    27412712
     
    31823153  while ((tor = tr_torrentNext (mgr->session, tor)))
    31833154    {
    3184       if (tor->isRunning)
     3155      if (tor->isRunning && tor->peerCount)
    31853156        {
    31863157          tr_swarm * s = tor->swarm;
    3187           if (!tr_ptrArrayEmpty (&s->peers))
    3188             {
    3189               rechokeUploads (s, now);
    3190               rechokeDownloads (s);
    3191             }
     3158          rechokeUploads (s, now);
     3159          rechokeDownloads (s);
    31923160        }
    31933161    }
     
    33223290
    33233291  removed = tr_ptrArrayRemoveSorted (&s->peers, peer, peerCompare);
     3292  --s->tor->peerCount;
     3293  --s->tor->peerFromCount[atom->fromFirst];
    33243294
    33253295  if (replicationExists (s))
     
    33273297
    33283298  assert (removed == peer);
     3299  assert (s->tor->peerCount == tr_ptrArraySize (&s->peers));
     3300  assert (s->tor->peerFromCount[atom->fromFirst] >= 0);
     3301
    33293302  tr_peerFree (removed);
    33303303}
     
    33633336  while (!tr_ptrArrayEmpty (&s->peers))
    33643337    removePeer (s, tr_ptrArrayNth (&s->peers, 0));
     3338
     3339  assert (!s->tor->peerCount);
    33653340}
    33663341
     
    36463621      if (tor->isStopping)
    36473622        tr_torrentStop (tor);
     3623
     3624      /* update the torrent's stats */
     3625      tor->activeWebseedCount = countActiveWebseeds (tor->swarm);
    36483626    }
    36493627
  • trunk/libtransmission/peer-mgr.h

    r13954 r14083  
    171171void         tr_peerMgrOnBlocklistChanged   (tr_peerMgr         * manager);
    172172
    173 void         tr_peerMgrTorrentStats         (tr_torrent         * tor,
    174                                              int                * setmePeersConnected,
    175                                              int                * setmeWebseedsSendingToUs,
    176                                              int                * setmePeersSendingToUs,
    177                                              int                * setmePeersGettingFromUs,
    178                                              int                * setmePeersFrom); /* TR_PEER_FROM__MAX */
    179 
    180173struct tr_peer_stat * tr_peerMgrPeerStats   (const tr_torrent   * tor,
    181174                                             int                * setmeCount);
  • trunk/libtransmission/peer-msgs.c

    r14076 r14083  
    199199  int prefetchCount;
    200200
     201  int is_active[2];
     202
    201203  /* how long the outMessages batch should be allowed to grow before
    202204   * it's flushed -- some messages (like requests >:) should be sent
     
    268270***
    269271**/
     272
     273static void
     274myDebug (const char * file, int line,
     275         const struct tr_peerMsgs * msgs,
     276         const char * fmt, ...) TR_GNUC_PRINTF(4, 5);
    270277
    271278static void
     
    679686#endif
    680687
     688/***
     689****  ACTIVE
     690***/
     691
     692bool
     693tr_peerMsgsIsActive (const tr_peerMsgs  * msgs, tr_direction direction)
     694{
     695  assert (tr_isPeerMsgs (msgs));
     696  assert (tr_isDirection (direction));
     697
     698  return msgs->is_active[direction];
     699}
     700
     701static void
     702tr_peerMsgsSetActive (tr_peerMsgs  * msgs,
     703                      tr_direction   direction,
     704                      bool           is_active)
     705{
     706  if (msgs->is_active[direction] != is_active)
     707    {
     708      int n = msgs->torrent->activePeerCount[direction];
     709
     710      msgs->is_active[direction] = is_active;
     711
     712      if (is_active)
     713        ++n;
     714      else
     715        --n;
     716      assert (0 <= n);
     717      assert (n <= msgs->torrent->peerCount);
     718
     719      msgs->torrent->activePeerCount[direction] = n;
     720    }
     721}
     722
     723void
     724tr_peerMsgsUpdateActive (tr_peerMsgs * msgs, tr_direction direction)
     725{
     726  bool active;
     727
     728  assert (tr_isPeerMsgs (msgs));
     729  assert (tr_isDirection (direction));
     730
     731  if (direction == TR_CLIENT_TO_PEER)
     732    {
     733      active = tr_peerMsgsIsPeerInterested (msgs)
     734           && !tr_peerMsgsIsPeerChoked (msgs);
     735    }
     736  else /* TR_PEER_TO_CLIENT */
     737    {
     738      if (!tr_torrentHasMetadata (msgs->torrent))
     739        active = true;
     740      else
     741        active = tr_peerMsgsIsClientInterested (msgs)
     742             && !tr_peerMsgsIsClientChoked (msgs);
     743    }
     744
     745  tr_peerMsgsSetActive (msgs, direction, active);
     746}
     747
    681748/**
    682749***  INTEREST
     
    712779
    713780  if (msgs->client_is_interested != b)
    714     sendInterest (msgs, b);
     781    {
     782      sendInterest (msgs, b);
     783
     784      tr_peerMsgsUpdateActive (msgs, TR_PEER_TO_CLIENT);
     785    }
    715786}
    716787
     
    14171488            if (!fext)
    14181489                fireGotChoke (msgs);
     1490            tr_peerMsgsUpdateActive (msgs, TR_PEER_TO_CLIENT);
    14191491            break;
    14201492
     
    14221494            dbgmsg (msgs, "got Unchoke");
    14231495            msgs->client_is_choked = false;
     1496            tr_peerMsgsUpdateActive (msgs, TR_PEER_TO_CLIENT);
    14241497            updateDesiredRequestCount (msgs);
    14251498            break;
     
    14281501            dbgmsg (msgs, "got Interested");
    14291502            msgs->peer_is_interested = true;
     1503            tr_peerMsgsUpdateActive (msgs, TR_CLIENT_TO_PEER);
    14301504            break;
    14311505
     
    14331507            dbgmsg (msgs, "got Not Interested");
    14341508            msgs->peer_is_interested = false;
     1509            tr_peerMsgsUpdateActive (msgs, TR_CLIENT_TO_PEER);
    14351510            break;
    14361511
     
    23942469  assert (msgs != NULL);
    23952470
     2471  tr_peerMsgsSetActive (msgs, TR_UP, false);
     2472  tr_peerMsgsSetActive (msgs, TR_DOWN, false);
     2473
    23962474  if (msgs->pexTimer != NULL)
    23972475    event_free (msgs->pexTimer);
     
    24182496{
    24192497  .destruct = peermsgs_destruct,
    2420   .is_transferring_pieces = peermsgs_is_transferring_pieces
     2498  .is_transferring_pieces = peermsgs_is_transferring_pieces,
    24212499};
    24222500
     
    25272605  m->client_is_interested = false;
    25282606  m->peer_is_interested = false;
     2607  m->is_active[TR_UP] = false;
     2608  m->is_active[TR_DOWN] = false;
    25292609  m->callback = callback;
    25302610  m->callbackData = callbackData;
  • trunk/libtransmission/peer-msgs.h

    r14076 r14083  
    5353bool         tr_peerMsgsIsClientInterested   (const tr_peerMsgs        * msgs);
    5454
     55bool         tr_peerMsgsIsActive             (const tr_peerMsgs        * msgs,
     56                                              tr_direction               direction);
     57
     58void         tr_peerMsgsUpdateActive         (tr_peerMsgs              * msgs,
     59                                              tr_direction               direction);
     60
    5561time_t       tr_peerMsgsGetConnectionAge     (const tr_peerMsgs        * msgs);
    5662
  • trunk/libtransmission/torrent.c

    r14079 r14083  
    12561256  unsigned int pieceUploadSpeed_Bps;
    12571257  unsigned int pieceDownloadSpeed_Bps;
     1258  int i;
    12581259
    12591260  assert (tr_isTorrent (tor));
     
    12711272  s->manualAnnounceTime = tr_announcerNextManualAnnounce (tor);
    12721273
    1273   tr_peerMgrTorrentStats (tor,
    1274                           &s->peersConnected,
    1275                           &s->webseedsSendingToUs,
    1276                           &s->peersSendingToUs,
    1277                           &s->peersGettingFromUs,
    1278                           s->peersFrom);
     1274  s->peersConnected      = tor->peerCount;
     1275  s->peersSendingToUs    = tor->activePeerCount[TR_DOWN];
     1276  s->peersGettingFromUs  = tor->activePeerCount[TR_UP];
     1277  s->webseedsSendingToUs = tor->activeWebseedCount;
     1278  for (i=0; i<TR_PEER_FROM__MAX; i++)
     1279    s->peersFrom[i] = tor->peerFromCount[i];
    12791280
    12801281  s->rawUploadSpeed_KBps     = toSpeedKBps (tr_bandwidthGetRawSpeed_Bps (&tor->bandwidth, now, TR_UP));
  • trunk/libtransmission/torrent.h

    r13933 r14083  
    226226    int                        secondsDownloading;
    227227    int                        secondsSeeding;
     228
     229    int                        peerCount;
     230    int                        peerFromCount[TR_PEER_FROM__MAX];
     231    int                        activePeerCount[2];
     232    int                        activeWebseedCount;
    228233
    229234    int                        queuePosition;
Note: See TracChangeset for help on using the changeset viewer.