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