| 26 | Lemon::GUI::ListColumn procCPUUsage = {.name = "CPU Usage", .displayWidth = 80}; |
| 27 | |
| 28 | int main(int argc, char** argv){ |
| 29 | window = new Lemon::GUI::Window("LemonMonitor", {360, 400}, 0, Lemon::GUI::WindowType::GUI); |
| 30 | |
| 31 | listView = new Lemon::GUI::ListView({0, 0, 0, 0}); |
| 32 | listView->AddColumn(procName); |
| 33 | listView->AddColumn(procID); |
| 34 | listView->AddColumn(procUptime); |
| 35 | listView->AddColumn(procCPUUsage); |
| 36 | listView->SetLayout(Lemon::GUI::LayoutSize::Stretch, Lemon::GUI::LayoutSize::Stretch); |
| 37 | |
| 38 | window->AddWidget(listView); |
| 39 | |
| 40 | std::vector<lemon_process_info_t> processes; |
| 41 | while(!window->closed){ |
| 42 | Lemon::GetProcessList(processes); |
| 43 | |
| 44 | uint64_t activeTimeSum = 0; |
| 45 | |
| 46 | for(lemon_process_info_t proc : processes){ |
| 47 | if(processTimer.find(proc.pid) != processTimer.end()){ |
| 48 | ProcessCPUTime& pTime = processTimer.at(proc.pid); |
| 49 | |
| 50 | uint64_t diff = proc.activeUs - pTime.activeUs; |
| 51 | activeTimeSum += diff; |
| 52 | |
| 53 | pTime.activeUs = proc.activeUs; // Update the entry |
| 54 | pTime.diff = diff; |
| 55 | } else { |
| 56 | processTimer[proc.pid] = {.diff = 0, .activeUs = proc.activeUs, .lastUsage = 0 }; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | listView->ClearItems(); |
| 61 | for(lemon_process_info_t proc : processes){ |
| 62 | |
| 63 | char uptime[40]; |
| 64 | snprintf(uptime, 39, "%lum %lus", proc.runningTime / 60, proc.runningTime % 60); |
| 65 | |
| 66 | char usage[6]; |
| 67 | if(processTimer.find(proc.pid) != processTimer.end()){ |
| 68 | ProcessCPUTime& pTime = processTimer.at(proc.pid); |
| 69 | |
| 70 | if(pTime.diff && activeTimeSum){ |
| 71 | snprintf(usage, 5, "%lu%%", (pTime.diff * 100) / activeTimeSum); // Multiply by 100 to get a percentage between 0 and 100 as opposed to 0 to 1 |
| 72 | pTime.lastUsage = static_cast<short>((pTime.diff * 100) / activeTimeSum); |
| 73 | } else { |
| 74 | strcpy(usage, "0%"); |
| 75 | pTime.lastUsage = 0; |
| 76 | } |
| 77 | } else { |
| 78 | strcpy(usage, "0%"); |
| 79 | |
| 80 | processTimer[proc.pid] = {.diff = 0, .activeUs = proc.activeUs, .lastUsage = 0 }; |
| 81 | } |
| 82 | |
| 83 | Lemon::GUI::ListItem pItem = {.details = {proc.name, std::to_string(proc.pid), uptime, usage}}; |
| 84 | listView->AddItem(pItem); |
| 85 | } |
nothing calls this directly
no test coverage detected