1 /* 2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "main_window/ThreadsPage.h" 8 9 #include <stdio.h> 10 11 #include <new> 12 13 #include "table/TableColumns.h" 14 15 16 // #pragma mark - ThreadsTableModel 17 18 19 class MainWindow::ThreadsPage::ThreadsTableModel : public TableModel { 20 public: 21 ThreadsTableModel(Model* model) 22 : 23 fModel(model) 24 { 25 } 26 27 virtual int32 CountColumns() const 28 { 29 return 16; 30 } 31 32 virtual int32 CountRows() const 33 { 34 return fModel->CountThreads(); 35 } 36 37 virtual bool GetValueAt(int32 rowIndex, int32 columnIndex, BVariant& value) 38 { 39 Model::Thread* thread = fModel->ThreadAt(rowIndex); 40 if (thread == NULL) 41 return false; 42 43 switch (columnIndex) { 44 case 0: 45 value.SetTo(thread->ID()); 46 return true; 47 case 1: 48 value.SetTo(thread->Name(), B_VARIANT_DONT_COPY_DATA); 49 return true; 50 case 2: 51 { 52 char buffer[128]; 53 Model::Team* team = thread->GetTeam(); 54 snprintf(buffer, sizeof(buffer), "%s (%ld)", team->Name(), 55 team->ID()); 56 value.SetTo(buffer); 57 return true; 58 } 59 case 3: 60 value.SetTo(thread->CreationTime()); 61 return true; 62 case 4: 63 value.SetTo(thread->DeletionTime()); 64 return true; 65 case 5: 66 value.SetTo(thread->Runs()); 67 return true; 68 case 6: 69 value.SetTo(thread->TotalRunTime()); 70 return true; 71 case 7: 72 value.SetTo(thread->Latencies()); 73 return true; 74 case 8: 75 value.SetTo(thread->TotalLatency()); 76 return true; 77 case 9: 78 value.SetTo(thread->Preemptions()); 79 return true; 80 case 10: 81 value.SetTo(thread->TotalRerunTime()); 82 return true; 83 case 11: 84 value.SetTo(thread->Waits()); 85 return true; 86 case 12: 87 value.SetTo(thread->TotalWaitTime()); 88 return true; 89 case 13: 90 value.SetTo(thread->UnspecifiedWaitTime()); 91 return true; 92 case 14: 93 value.SetTo(thread->IOCount()); 94 return true; 95 case 15: 96 value.SetTo(thread->IOTime()); 97 return true; 98 default: 99 return false; 100 } 101 } 102 103 private: 104 Model* fModel; 105 }; 106 107 108 // #pragma mark - ThreadsPage 109 110 111 MainWindow::ThreadsPage::ThreadsPage(MainWindow* parent) 112 : 113 BGroupView(B_VERTICAL), 114 fParent(parent), 115 fThreadsTable(NULL), 116 fThreadsTableModel(NULL), 117 fModel(NULL) 118 { 119 SetName("Threads"); 120 121 fThreadsTable = new Table("threads list", 0); 122 AddChild(fThreadsTable->ToView()); 123 124 fThreadsTable->AddColumn(new Int32TableColumn(0, "ID", 40, 20, 1000, 125 B_TRUNCATE_MIDDLE, B_ALIGN_RIGHT)); 126 fThreadsTable->AddColumn(new StringTableColumn(1, "Name", 80, 40, 1000, 127 B_TRUNCATE_END, B_ALIGN_LEFT)); 128 fThreadsTable->AddColumn(new StringTableColumn(2, "Team", 80, 40, 1000, 129 B_TRUNCATE_END, B_ALIGN_LEFT)); 130 fThreadsTable->AddColumn(new NanotimeTableColumn(3, "Creation", 80, 40, 131 1000, true, B_TRUNCATE_MIDDLE, B_ALIGN_RIGHT)); 132 fThreadsTable->AddColumn(new NanotimeTableColumn(4, "Deletion", 80, 40, 133 1000, false, B_TRUNCATE_MIDDLE, B_ALIGN_RIGHT)); 134 fThreadsTable->AddColumn(new Int64TableColumn(5, "Runs", 80, 20, 1000, 135 B_TRUNCATE_END, B_ALIGN_RIGHT)); 136 fThreadsTable->AddColumn(new NanotimeTableColumn(6, "Run time", 80, 20, 137 1000, false, B_TRUNCATE_END, B_ALIGN_RIGHT)); 138 fThreadsTable->AddColumn(new Int64TableColumn(7, "Latencies", 80, 20, 1000, 139 B_TRUNCATE_END, B_ALIGN_RIGHT)); 140 fThreadsTable->AddColumn(new NanotimeTableColumn(8, "Latency time", 80, 20, 141 1000, B_TRUNCATE_END, B_ALIGN_RIGHT)); 142 fThreadsTable->AddColumn(new Int64TableColumn(9, "Preemptions", 80, 20, 143 1000, B_TRUNCATE_END, B_ALIGN_RIGHT)); 144 fThreadsTable->AddColumn(new NanotimeTableColumn(10, "Preemption time", 80, 145 20, 1000, false, B_TRUNCATE_END, B_ALIGN_RIGHT)); 146 fThreadsTable->AddColumn(new Int64TableColumn(11, "Waits", 80, 20, 147 1000, B_TRUNCATE_END, B_ALIGN_RIGHT)); 148 fThreadsTable->AddColumn(new NanotimeTableColumn(12, "Wait time", 80, 149 20, 1000, false, B_TRUNCATE_END, B_ALIGN_RIGHT)); 150 fThreadsTable->AddColumn(new NanotimeTableColumn(13, "Unspecified time", 80, 151 20, 1000, false, B_TRUNCATE_END, B_ALIGN_RIGHT)); 152 fThreadsTable->AddColumn(new Int64TableColumn(14, "I/O Count", 80, 20, 153 1000, B_TRUNCATE_END, B_ALIGN_RIGHT)); 154 fThreadsTable->AddColumn(new NanotimeTableColumn(15, "I/O Time", 80, 155 20, 1000, false, B_TRUNCATE_END, B_ALIGN_RIGHT)); 156 157 fThreadsTable->AddTableListener(this); 158 } 159 160 161 MainWindow::ThreadsPage::~ThreadsPage() 162 { 163 fThreadsTable->SetTableModel(NULL); 164 delete fThreadsTableModel; 165 } 166 167 168 void 169 MainWindow::ThreadsPage::SetModel(Model* model) 170 { 171 if (model == fModel) 172 return; 173 174 if (fModel != NULL) { 175 fThreadsTable->SetTableModel(NULL); 176 delete fThreadsTableModel; 177 fThreadsTableModel = NULL; 178 } 179 180 fModel = model; 181 182 if (fModel != NULL) { 183 fThreadsTableModel = new(std::nothrow) ThreadsTableModel(fModel); 184 fThreadsTable->SetTableModel(fThreadsTableModel); 185 fThreadsTable->ResizeAllColumnsToPreferred(); 186 } 187 } 188 189 190 void 191 MainWindow::ThreadsPage::TableRowInvoked(Table* table, int32 rowIndex) 192 { 193 Model::Thread* thread = fModel->ThreadAt(rowIndex); 194 if (thread != NULL) 195 fParent->OpenThreadWindow(thread); 196 } 197