Home | History | Annotate | Download | only in net_internals
      1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 /**
      6  * SocketPoolWrapper is a wrapper around socket pools entries.  It's
      7  * used by the log and sockets view to print tables containing both
      8  * a synopsis of the state of all pools, and listing the groups within
      9  * individual pools.
     10  *
     11  * The constructor takes a socket pool and its parent, and generates a
     12  * unique name from the two, which is stored as |fullName|.  |parent|
     13  * must itself be a SocketPoolWrapper.
     14  *
     15  * @constructor
     16  */
     17 function SocketPoolWrapper(socketPool, parent) {
     18   this.origPool = socketPool;
     19   this.fullName = socketPool.name;
     20   if (this.fullName != socketPool.type)
     21     this.fullName += ' (' + socketPool.type + ')';
     22   if (parent)
     23     this.fullName = parent.fullName + '->' + this.fullName;
     24 }
     25 
     26 /**
     27  * Returns an array of SocketPoolWrappers created from each of the socket pools
     28  * in |socketPoolInfo|.  Nested socket pools appear immediately after their
     29  * parent, and groups of nodes from trees with root nodes with the same id are
     30  * placed adjacent to each other.
     31  */
     32 SocketPoolWrapper.createArrayFrom = function(socketPoolInfo) {
     33   // Create SocketPoolWrappers for each socket pool and separate socket pools
     34   // them into different arrays based on root node name.
     35   var socketPoolGroups = [];
     36   var socketPoolNameLists = {};
     37   for (var i = 0; i < socketPoolInfo.length; ++i) {
     38     var name = socketPoolInfo[i].name;
     39     if (!socketPoolNameLists[name]) {
     40       socketPoolNameLists[name] = [];
     41       socketPoolGroups.push(socketPoolNameLists[name]);
     42     }
     43     SocketPoolWrapper.addSocketPoolsToList_(socketPoolNameLists[name],
     44                                             socketPoolInfo[i], null);
     45   }
     46 
     47   // Merge the arrays.
     48   var socketPoolList = [];
     49   for (var i = 0; i < socketPoolGroups.length; ++i) {
     50     socketPoolList = socketPoolList.concat(socketPoolGroups[i]);
     51   }
     52   return socketPoolList;
     53 };
     54 
     55 /**
     56  * Recursively creates SocketPoolWrappers from |origPool| and all its
     57  * children and adds them all to |socketPoolList|.  |parent| is the
     58  * SocketPoolWrapper for the parent of |origPool|, or null, if it's
     59  * a top level socket pool.
     60  */
     61 SocketPoolWrapper.addSocketPoolsToList_ = function(socketPoolList,
     62                                                    origPool,
     63                                                    parent) {
     64   var socketPool = new SocketPoolWrapper(origPool, parent);
     65   socketPoolList.push(socketPool);
     66   if (origPool.nested_pools) {
     67     for (var i = 0; i < origPool.nested_pools.length; ++i) {
     68       SocketPoolWrapper.addSocketPoolsToList_(socketPoolList,
     69                                               origPool.nested_pools[i],
     70                                               socketPool);
     71     }
     72   }
     73 };
     74 
     75 /**
     76  * Returns a table printer containing information on each
     77  * SocketPoolWrapper in |socketPools|.
     78  */
     79 SocketPoolWrapper.createTablePrinter = function(socketPools) {
     80   var tablePrinter = new TablePrinter();
     81   tablePrinter.addHeaderCell('Name');
     82   tablePrinter.addHeaderCell('Handed Out');
     83   tablePrinter.addHeaderCell('Idle');
     84   tablePrinter.addHeaderCell('Connecting');
     85   tablePrinter.addHeaderCell('Max');
     86   tablePrinter.addHeaderCell('Max Per Group');
     87   tablePrinter.addHeaderCell('Generation');
     88 
     89   for (var i = 0; i < socketPools.length; i++) {
     90     var origPool = socketPools[i].origPool;
     91 
     92     tablePrinter.addRow();
     93     tablePrinter.addCell(socketPools[i].fullName);
     94 
     95     tablePrinter.addCell(origPool.handed_out_socket_count);
     96     var idleCell = tablePrinter.addCell(origPool.idle_socket_count);
     97     var connectingCell = tablePrinter.addCell(origPool.connecting_socket_count);
     98 
     99     if (origPool.groups) {
    100       var idleSources = [];
    101       var connectingSources = [];
    102       for (var groupName in origPool.groups) {
    103         var group = origPool.groups[groupName];
    104         idleSources = idleSources.concat(group.idle_sockets);
    105         connectingSources = connectingSources.concat(group.connect_jobs);
    106       }
    107       idleCell.link = SocketPoolWrapper.sourceListLink(idleSources);
    108       connectingCell.link =
    109           SocketPoolWrapper.sourceListLink(connectingSources);
    110     }
    111 
    112     tablePrinter.addCell(origPool.max_socket_count);
    113     tablePrinter.addCell(origPool.max_sockets_per_group);
    114     tablePrinter.addCell(origPool.pool_generation_number);
    115   }
    116   return tablePrinter;
    117 };
    118 
    119 /**
    120  * Returns a table printer containing information on all a
    121  * socket pool's groups.
    122  */
    123 SocketPoolWrapper.prototype.createGroupTablePrinter = function() {
    124   var tablePrinter = new TablePrinter();
    125   tablePrinter.setTitle(this.fullName);
    126 
    127   tablePrinter.addHeaderCell('Name');
    128   tablePrinter.addHeaderCell('Pending');
    129   tablePrinter.addHeaderCell('Top Priority');
    130   tablePrinter.addHeaderCell('Active');
    131   tablePrinter.addHeaderCell('Idle');
    132   tablePrinter.addHeaderCell('Connect Jobs');
    133   tablePrinter.addHeaderCell('Backup Job');
    134   tablePrinter.addHeaderCell('Stalled');
    135 
    136   for (var groupName in this.origPool.groups) {
    137     var group = this.origPool.groups[groupName];
    138 
    139     tablePrinter.addRow();
    140     tablePrinter.addCell(groupName);
    141     tablePrinter.addCell(group.pending_request_count);
    142     if (group.top_pending_priority != undefined)
    143       tablePrinter.addCell(group.top_pending_priority);
    144     else
    145       tablePrinter.addCell('-');
    146 
    147     tablePrinter.addCell(group.active_socket_count);
    148     var idleCell = tablePrinter.addCell(group.idle_sockets.length);
    149     var connectingCell = tablePrinter.addCell(group.connect_jobs.length);
    150 
    151     idleCell.link = SocketPoolWrapper.sourceListLink(group.idle_sockets);
    152     connectingCell.link =
    153         SocketPoolWrapper.sourceListLink(group.connect_jobs);
    154 
    155     tablePrinter.addCell(group.has_backup_job);
    156     tablePrinter.addCell(group.is_stalled);
    157   }
    158   return tablePrinter;
    159 };
    160 
    161 /**
    162  * Takes in a list of source IDs and returns a link that will select the
    163  * specified sources.
    164  */
    165 SocketPoolWrapper.sourceListLink = function(sources) {
    166   if (!sources.length)
    167     return null;
    168   return '#events&q=id:' + sources.join('%20id:');
    169 }
    170