stats_viewer.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // Copyright (c) 2012 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. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Diagnostics;
  10. using System.Drawing;
  11. using System.Text;
  12. using System.Windows.Forms;
  13. using System.IO;
  14. namespace StatsViewer {
  15. public partial class StatsViewer : Form {
  16. /// <summary>
  17. /// Create a StatsViewer.
  18. /// </summary>
  19. public StatsViewer() {
  20. InitializeComponent();
  21. }
  22. #region Protected Methods
  23. /// <summary>
  24. /// Callback when the form loads.
  25. /// </summary>
  26. /// <param name="e"></param>
  27. protected override void OnLoad(EventArgs e) {
  28. base.OnLoad(e);
  29. timer_ = new Timer();
  30. timer_.Interval = kPollInterval;
  31. timer_.Tick += new EventHandler(PollTimerTicked);
  32. timer_.Start();
  33. }
  34. #endregion
  35. #region Private Methods
  36. /// <summary>
  37. /// Attempt to open the stats file.
  38. /// Return true on success, false otherwise.
  39. /// </summary>
  40. private bool OpenStatsFile() {
  41. StatsTable table = new StatsTable();
  42. if (table.Open(kStatsTableName)) {
  43. stats_table_ = table;
  44. return true;
  45. }
  46. return false;
  47. }
  48. /// <summary>
  49. /// Close the open stats file.
  50. /// </summary>
  51. private void CloseStatsFile() {
  52. if (this.stats_table_ != null)
  53. {
  54. this.stats_table_.Close();
  55. this.stats_table_ = null;
  56. this.listViewCounters.Items.Clear();
  57. }
  58. }
  59. /// <summary>
  60. /// Updates the process list in the UI.
  61. /// </summary>
  62. private void UpdateProcessList() {
  63. int current_pids = comboBoxFilter.Items.Count;
  64. int table_pids = stats_table_.Processes.Count;
  65. if (current_pids != table_pids + 1) // Add one because of the "all" entry.
  66. {
  67. int selected_index = this.comboBoxFilter.SelectedIndex;
  68. this.comboBoxFilter.Items.Clear();
  69. this.comboBoxFilter.Items.Add(kStringAllProcesses);
  70. foreach (int pid in stats_table_.Processes)
  71. this.comboBoxFilter.Items.Add(kStringProcess + pid.ToString());
  72. this.comboBoxFilter.SelectedIndex = selected_index;
  73. }
  74. }
  75. /// <summary>
  76. /// Updates the UI for a counter.
  77. /// </summary>
  78. /// <param name="counter"></param>
  79. private void UpdateCounter(IStatsCounter counter) {
  80. ListView view;
  81. // Figure out which list this counter goes into.
  82. if (counter is StatsCounterRate)
  83. view = listViewRates;
  84. else if (counter is StatsCounter || counter is StatsTimer)
  85. view = listViewCounters;
  86. else
  87. return; // Counter type not supported yet.
  88. // See if the counter is already in the list.
  89. ListViewItem item = view.Items[counter.name];
  90. if (item != null)
  91. {
  92. // Update an existing counter.
  93. Debug.Assert(item is StatsCounterListViewItem);
  94. StatsCounterListViewItem counter_item = item as StatsCounterListViewItem;
  95. counter_item.Update(counter, filter_pid_);
  96. }
  97. else
  98. {
  99. // Create a new counter
  100. StatsCounterListViewItem new_item = null;
  101. if (counter is StatsCounterRate)
  102. new_item = new RateListViewItem(counter, filter_pid_);
  103. else if (counter is StatsCounter || counter is StatsTimer)
  104. new_item = new CounterListViewItem(counter, filter_pid_);
  105. Debug.Assert(new_item != null);
  106. view.Items.Add(new_item);
  107. }
  108. }
  109. /// <summary>
  110. /// Sample the data and update the UI
  111. /// </summary>
  112. private void SampleData() {
  113. // If the table isn't open, try to open it again.
  114. if (stats_table_ == null)
  115. if (!OpenStatsFile())
  116. return;
  117. if (stats_counters_ == null)
  118. stats_counters_ = stats_table_.Counters();
  119. if (pause_updates_)
  120. return;
  121. stats_counters_.Update();
  122. UpdateProcessList();
  123. foreach (IStatsCounter counter in stats_counters_)
  124. UpdateCounter(counter);
  125. }
  126. /// <summary>
  127. /// Set the background color based on the value
  128. /// </summary>
  129. /// <param name="item"></param>
  130. /// <param name="value"></param>
  131. private void ColorItem(ListViewItem item, int value)
  132. {
  133. if (value < 0)
  134. item.ForeColor = Color.Red;
  135. else if (value > 0)
  136. item.ForeColor = Color.DarkGreen;
  137. else
  138. item.ForeColor = Color.Black;
  139. }
  140. /// <summary>
  141. /// Called when the timer fires.
  142. /// </summary>
  143. /// <param name="sender"></param>
  144. /// <param name="e"></param>
  145. void PollTimerTicked(object sender, EventArgs e) {
  146. SampleData();
  147. }
  148. /// <summary>
  149. /// Called when the interval is changed by the user.
  150. /// </summary>
  151. /// <param name="sender"></param>
  152. /// <param name="e"></param>
  153. private void interval_changed(object sender, EventArgs e) {
  154. int interval = 1;
  155. if (int.TryParse(comboBoxInterval.Text, out interval)) {
  156. if (timer_ != null) {
  157. timer_.Stop();
  158. timer_.Interval = interval * 1000;
  159. timer_.Start();
  160. }
  161. } else {
  162. comboBoxInterval.Text = timer_.Interval.ToString();
  163. }
  164. }
  165. /// <summary>
  166. /// Called when the user changes the filter
  167. /// </summary>
  168. /// <param name="sender"></param>
  169. /// <param name="e"></param>
  170. private void filter_changed(object sender, EventArgs e) {
  171. // While in this event handler, don't allow recursive events!
  172. this.comboBoxFilter.SelectedIndexChanged -= new System.EventHandler(this.filter_changed);
  173. if (this.comboBoxFilter.Text == kStringAllProcesses)
  174. filter_pid_ = 0;
  175. else
  176. int.TryParse(comboBoxFilter.Text.Substring(kStringProcess.Length), out filter_pid_);
  177. SampleData();
  178. this.comboBoxFilter.SelectedIndexChanged += new System.EventHandler(this.filter_changed);
  179. }
  180. /// <summary>
  181. /// Callback when the mouse enters a control
  182. /// </summary>
  183. /// <param name="sender"></param>
  184. /// <param name="e"></param>
  185. private void mouse_Enter(object sender, EventArgs e) {
  186. // When the dropdown is expanded, we pause
  187. // updates, as it messes with the UI.
  188. pause_updates_ = true;
  189. }
  190. /// <summary>
  191. /// Callback when the mouse leaves a control
  192. /// </summary>
  193. /// <param name="sender"></param>
  194. /// <param name="e"></param>
  195. private void mouse_Leave(object sender, EventArgs e) {
  196. pause_updates_ = false;
  197. }
  198. /// <summary>
  199. /// Called when the user clicks the zero-stats button.
  200. /// </summary>
  201. /// <param name="sender"></param>
  202. /// <param name="e"></param>
  203. private void buttonZero_Click(object sender, EventArgs e) {
  204. this.stats_table_.Zero();
  205. SampleData();
  206. }
  207. /// <summary>
  208. /// Called when the user clicks a column heading.
  209. /// </summary>
  210. /// <param name="sender"></param>
  211. /// <param name="e"></param>
  212. private void column_Click(object sender, ColumnClickEventArgs e) {
  213. if (e.Column != sort_column_) {
  214. sort_column_ = e.Column;
  215. this.listViewCounters.Sorting = SortOrder.Ascending;
  216. } else {
  217. if (this.listViewCounters.Sorting == SortOrder.Ascending)
  218. this.listViewCounters.Sorting = SortOrder.Descending;
  219. else
  220. this.listViewCounters.Sorting = SortOrder.Ascending;
  221. }
  222. this.listViewCounters.ListViewItemSorter =
  223. new ListViewItemComparer(e.Column, this.listViewCounters.Sorting);
  224. this.listViewCounters.Sort();
  225. }
  226. /// <summary>
  227. /// Called when the user clicks the button "Export".
  228. /// </summary>
  229. /// <param name="sender"></param>
  230. /// <param name="e"></param>
  231. private void buttonExport_Click(object sender, EventArgs e) {
  232. //Have to pick a textfile to export to.
  233. //Saves what is shown in listViewStats in the format: function value
  234. //(with a tab in between), so that it is easy to copy paste into a spreadsheet.
  235. //(Does not save the delta values.)
  236. TextWriter tw = null;
  237. try {
  238. saveFileDialogExport.CheckFileExists = false;
  239. saveFileDialogExport.ShowDialog();
  240. tw = new StreamWriter(saveFileDialogExport.FileName);
  241. for (int i = 0; i < listViewCounters.Items.Count; i++) {
  242. tw.Write(listViewCounters.Items[i].SubItems[0].Text + "\t");
  243. tw.WriteLine(listViewCounters.Items[i].SubItems[1].Text);
  244. }
  245. }
  246. catch (IOException ex) {
  247. MessageBox.Show(string.Format("There was an error while saving your results file. The results might not have been saved correctly.: {0}", ex.Message));
  248. }
  249. finally{
  250. if (tw != null) tw.Close();
  251. }
  252. }
  253. #endregion
  254. class ListViewItemComparer : IComparer {
  255. public ListViewItemComparer() {
  256. this.col_ = 0;
  257. this.order_ = SortOrder.Ascending;
  258. }
  259. public ListViewItemComparer(int column, SortOrder order) {
  260. this.col_ = column;
  261. this.order_ = order;
  262. }
  263. public int Compare(object x, object y) {
  264. int return_value = -1;
  265. object x_tag = ((ListViewItem)x).SubItems[col_].Tag;
  266. object y_tag = ((ListViewItem)y).SubItems[col_].Tag;
  267. if (Comparable(x_tag, y_tag))
  268. return_value = ((IComparable)x_tag).CompareTo(y_tag);
  269. else
  270. return_value = String.Compare(((ListViewItem)x).SubItems[col_].Text,
  271. ((ListViewItem)y).SubItems[col_].Text);
  272. if (order_ == SortOrder.Descending)
  273. return_value *= -1;
  274. return return_value;
  275. }
  276. #region Private Methods
  277. private bool Comparable(object x, object y) {
  278. if (x == null || y == null)
  279. return false;
  280. return x is IComparable && y is IComparable;
  281. }
  282. #endregion
  283. #region Private Members
  284. private int col_;
  285. private SortOrder order_;
  286. #endregion
  287. }
  288. #region Private Members
  289. private const string kStringAllProcesses = "All Processes";
  290. private const string kStringProcess = "Process ";
  291. private const int kPollInterval = 1000; // 1 second
  292. private const string kStatsTableName = "ChromeStats";
  293. private StatsTable stats_table_;
  294. private StatsTableCounters stats_counters_;
  295. private Timer timer_;
  296. private int filter_pid_;
  297. private bool pause_updates_;
  298. private int sort_column_ = -1;
  299. #endregion
  300. #region Private Event Callbacks
  301. private void openToolStripMenuItem_Click(object sender, EventArgs e)
  302. {
  303. OpenDialog dialog = new OpenDialog();
  304. dialog.ShowDialog();
  305. CloseStatsFile();
  306. StatsTable table = new StatsTable();
  307. bool rv = table.Open(dialog.FileName);
  308. if (!rv)
  309. {
  310. MessageBox.Show("Could not open statsfile: " + dialog.FileName);
  311. }
  312. else
  313. {
  314. stats_table_ = table;
  315. }
  316. }
  317. private void closeToolStripMenuItem_Click(object sender, EventArgs e)
  318. {
  319. CloseStatsFile();
  320. }
  321. private void quitToolStripMenuItem_Click(object sender, EventArgs e)
  322. {
  323. Application.Exit();
  324. }
  325. #endregion
  326. }
  327. /// <summary>
  328. /// Base class for counter list view items.
  329. /// </summary>
  330. internal class StatsCounterListViewItem : ListViewItem
  331. {
  332. /// <summary>
  333. /// Create the ListViewItem
  334. /// </summary>
  335. /// <param name="text"></param>
  336. public StatsCounterListViewItem(string text) : base(text) { }
  337. /// <summary>
  338. /// Update the ListViewItem given a new counter value.
  339. /// </summary>
  340. /// <param name="counter"></param>
  341. /// <param name="filter_pid"></param>
  342. public virtual void Update(IStatsCounter counter, int filter_pid) { }
  343. /// <summary>
  344. /// Set the background color based on the value
  345. /// </summary>
  346. /// <param name="value"></param>
  347. protected void ColorItem(int value)
  348. {
  349. if (value < 0)
  350. ForeColor = Color.Red;
  351. else if (value > 0)
  352. ForeColor = Color.DarkGreen;
  353. else
  354. ForeColor = Color.Black;
  355. }
  356. /// <summary>
  357. /// Create a new subitem with a zeroed Tag.
  358. /// </summary>
  359. /// <returns></returns>
  360. protected ListViewSubItem NewSubItem()
  361. {
  362. ListViewSubItem item = new ListViewSubItem();
  363. item.Tag = -1; // Arbitrarily initialize to -1.
  364. return item;
  365. }
  366. /// <summary>
  367. /// Set the value for a subitem.
  368. /// </summary>
  369. /// <param name="item"></param>
  370. /// <param name="val"></param>
  371. /// <returns>True if the value changed, false otherwise</returns>
  372. protected bool SetSubItem(ListViewSubItem item, int val)
  373. {
  374. // The reason for doing this extra compare is because
  375. // we introduce flicker if we unnecessarily update the
  376. // subitems. The UI is much less likely to cause you
  377. // a seizure when we do this.
  378. if (val != (int)item.Tag)
  379. {
  380. item.Text = val.ToString();
  381. item.Tag = val;
  382. return true;
  383. }
  384. return false;
  385. }
  386. }
  387. /// <summary>
  388. /// A listview item which contains a rate.
  389. /// </summary>
  390. internal class RateListViewItem : StatsCounterListViewItem
  391. {
  392. public RateListViewItem(IStatsCounter ctr, int filter_pid) :
  393. base(ctr.name)
  394. {
  395. StatsCounterRate rate = ctr as StatsCounterRate;
  396. Name = rate.name;
  397. SubItems.Add(NewSubItem());
  398. SubItems.Add(NewSubItem());
  399. SubItems.Add(NewSubItem());
  400. Update(ctr, filter_pid);
  401. }
  402. public override void Update(IStatsCounter counter, int filter_pid)
  403. {
  404. Debug.Assert(counter is StatsCounterRate);
  405. StatsCounterRate new_rate = counter as StatsCounterRate;
  406. int new_count = new_rate.GetCount(filter_pid);
  407. int new_time = new_rate.GetTime(filter_pid);
  408. int old_avg = Tag != null ? (int)Tag : 0;
  409. int new_avg = new_count > 0 ? (new_time / new_count) : 0;
  410. int delta = new_avg - old_avg;
  411. SetSubItem(SubItems[column_count_index], new_count);
  412. SetSubItem(SubItems[column_time_index], new_time);
  413. if (SetSubItem(SubItems[column_avg_index], new_avg))
  414. ColorItem(delta);
  415. Tag = new_avg;
  416. }
  417. private const int column_count_index = 1;
  418. private const int column_time_index = 2;
  419. private const int column_avg_index = 3;
  420. }
  421. /// <summary>
  422. /// A listview item which contains a counter.
  423. /// </summary>
  424. internal class CounterListViewItem : StatsCounterListViewItem
  425. {
  426. public CounterListViewItem(IStatsCounter ctr, int filter_pid) :
  427. base(ctr.name)
  428. {
  429. Name = ctr.name;
  430. SubItems.Add(NewSubItem());
  431. SubItems.Add(NewSubItem());
  432. Update(ctr, filter_pid);
  433. }
  434. public override void Update(IStatsCounter counter, int filter_pid) {
  435. Debug.Assert(counter is StatsCounter || counter is StatsTimer);
  436. int new_value = 0;
  437. if (counter is StatsCounter)
  438. new_value = ((StatsCounter)counter).GetValue(filter_pid);
  439. else if (counter is StatsTimer)
  440. new_value = ((StatsTimer)counter).GetValue(filter_pid);
  441. int old_value = Tag != null ? (int)Tag : 0;
  442. int delta = new_value - old_value;
  443. SetSubItem(SubItems[column_value_index], new_value);
  444. if (SetSubItem(SubItems[column_delta_index], delta))
  445. ColorItem(delta);
  446. Tag = new_value;
  447. }
  448. private const int column_value_index = 1;
  449. private const int column_delta_index = 2;
  450. }
  451. }