Home | History | Annotate | Download | only in net_internals
      1 // Copyright (c) 2011 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 var SocketPoolWrapper = (function() {
      6   'use strict';
      7 
      8   /**
      9    * SocketPoolWrapper is a wrapper around socket pools entries.  It's
     10    * used by the log and sockets view to print tables containing both
     11    * a synopsis of the state of all pools, and listing the groups within
     12    * individual pools.
     13    *
     14    * The constructor takes a socket pool and its parent, and generates a
     15    * unique name from the two, which is stored as |fullName|.  |parent|
     16    * must itself be a SocketPoolWrapper.
     17    *
     18    * @constructor
     19    */
     20   function SocketPoolWrapper(socketPool, parent) {
     21     this.origPool = socketPool;
     22     this.fullName = socketPool.name;
     23     if (this.fullName != socketPool.type)
     24       this.fullName += ' (' + socketPool.type + ')';
     25     if (parent)
     26       this.fullName = parent.fullName + '->' + this.fullName;
     27   }
     28 
     29   /**
     30    * Returns an array of SocketPoolWrappers created from each of the socket
     31    * pools in |socketPoolInfo|.  Nested socket pools appear immediately after
     32    * their parent, and groups of nodes from trees with root nodes with the same
     33    * id are placed adjacent to each other.
     34    */
     35   SocketPoolWrapper.createArrayFrom = function(socketPoolInfo) {
     36     // Create SocketPoolWrappers for each socket pool and separate socket pools
     37     // them into different arrays based on root node name.
     38     var socketPoolGroups = [];
     39     var socketPoolNameLists = {};
     40     for (var i = 0; i < socketPoolInfo.length; ++i) {
     41       var name = socketPoolInfo[i].name;
     42       if (!socketPoolNameLists[name]) {
     43         socketPoolNameLists[name] = [];
     44         socketPoolGroups.push(socketPoolNameLists[name]);
     45       }
     46       addSocketPoolsToList(socketPoolNameLists[name], socketPoolInfo[i], null);
     47     }
     48 
     49     // Merge the arrays.
     50     var socketPoolList = [];
     51     for (var i = 0; i < socketPoolGroups.length; ++i) {
     52       socketPoolList = socketPoolList.concat(socketPoolGroups[i]);
     53     }
     54     return socketPoolList;
     55   };
     56 
     57   /**
     58    * Recursively creates SocketPoolWrappers from |origPool| and all its
     59    * children and adds them all to |socketPoolList|.  |parent| is the
     60    * SocketPoolWrapper for the parent of |origPool|, or null, if it's
     61    * a top level socket pool.
     62    */
     63   function addSocketPoolsToList(socketPoolList, origPool, 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         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 =
     98           tablePrinter.addCell(origPool.connecting_socket_count);
     99 
    100       if (origPool.groups) {
    101         var idleSources = [];
    102         var connectingSources = [];
    103         for (var groupName in origPool.groups) {
    104           var group = origPool.groups[groupName];
    105           idleSources = idleSources.concat(group.idle_sockets);
    106           connectingSources = connectingSources.concat(group.connect_jobs);
    107         }
    108         idleCell.link = sourceListLink(idleSources);
    109         connectingCell.link = 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   SocketPoolWrapper.prototype = {
    120     /**
    121      * Returns a table printer containing information on all a
    122      * socket pool's groups.
    123      */
    124     createGroupTablePrinter: function() {
    125       var tablePrinter = new TablePrinter();
    126       tablePrinter.setTitle(this.fullName);
    127 
    128       tablePrinter.addHeaderCell('Name');
    129       tablePrinter.addHeaderCell('Pending');
    130       tablePrinter.addHeaderCell('Top Priority');
    131       tablePrinter.addHeaderCell('Active');
    132       tablePrinter.addHeaderCell('Idle');
    133       tablePrinter.addHeaderCell('Connect Jobs');
    134       tablePrinter.addHeaderCell('Backup Timer');
    135       tablePrinter.addHeaderCell('Stalled');
    136 
    137       for (var groupName in this.origPool.groups) {
    138         var group = this.origPool.groups[groupName];
    139 
    140         tablePrinter.addRow();
    141         tablePrinter.addCell(groupName);
    142         tablePrinter.addCell(group.pending_request_count);
    143         if (group.top_pending_priority != undefined)
    144           tablePrinter.addCell(group.top_pending_priority);
    145         else
    146           tablePrinter.addCell('-');
    147 
    148         tablePrinter.addCell(group.active_socket_count);
    149         var idleCell = tablePrinter.addCell(group.idle_sockets.length);
    150         var connectingCell = tablePrinter.addCell(group.connect_jobs.length);
    151 
    152         idleCell.link = sourceListLink(group.idle_sockets);
    153         connectingCell.link = sourceListLink(group.connect_jobs);
    154 
    155         tablePrinter.addCell(
    156             group.backup_job_timer_is_running ? 'started' : 'stopped');
    157         tablePrinter.addCell(group.is_stalled);
    158       }
    159       return tablePrinter;
    160     }
    161   };
    162 
    163   /**
    164    * Takes in a list of source IDs and returns a link that will select the
    165    * specified sources.
    166    */
    167   function sourceListLink(sources) {
    168     if (!sources.length)
    169       return null;
    170     return '#events&q=id:' + sources.join(',');
    171   }
    172 
    173   return SocketPoolWrapper;
    174 })();
    175