main.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <AK/CircularQueue.h>
  2. #include <LibCore/CProcessStatisticsReader.h>
  3. #include <LibCore/CTimer.h>
  4. #include <LibDraw/GraphicsBitmap.h>
  5. #include <LibGUI/GApplication.h>
  6. #include <LibGUI/GPainter.h>
  7. #include <LibGUI/GWindowServerConnection.h>
  8. NonnullRefPtr<GraphicsBitmap> create_shared_bitmap(const Size& size)
  9. {
  10. ASSERT(GWindowServerConnection::the().server_pid());
  11. ASSERT(!size.is_empty());
  12. size_t pitch = round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16);
  13. size_t size_in_bytes = size.height() * pitch;
  14. auto shared_buffer = SharedBuffer::create_with_size(size_in_bytes);
  15. ASSERT(shared_buffer);
  16. shared_buffer->share_with(GWindowServerConnection::the().server_pid());
  17. return GraphicsBitmap::create_with_shared_buffer(GraphicsBitmap::Format::RGBA32, *shared_buffer, size);
  18. }
  19. static void get_cpu_usage(unsigned& busy, unsigned& idle)
  20. {
  21. busy = 0;
  22. idle = 0;
  23. auto all_processes = CProcessStatisticsReader::get_all();
  24. for (auto& it : all_processes) {
  25. for (auto& jt : it.value.threads) {
  26. if (it.value.pid == 0)
  27. idle += jt.times_scheduled;
  28. else
  29. busy += jt.times_scheduled;
  30. }
  31. }
  32. }
  33. int main(int argc, char** argv)
  34. {
  35. GApplication app(argc, argv);
  36. Size applet_size(30, 16);
  37. Rect applet_rect({}, applet_size);
  38. CircularQueue<float, 30> cpu_history;
  39. i32 applet_id = GWindowServerConnection::the().send_sync<WindowServer::CreateMenuApplet>(applet_size)->applet_id();
  40. auto bitmap = create_shared_bitmap(applet_size);
  41. GWindowServerConnection::the().send_sync<WindowServer::SetMenuAppletBackingStore>(applet_id, bitmap->shared_buffer_id());
  42. unsigned last_busy = 0;
  43. unsigned last_idle = 0;
  44. auto repaint = [&] {
  45. unsigned busy;
  46. unsigned idle;
  47. get_cpu_usage(busy, idle);
  48. unsigned busy_diff = busy - last_busy;
  49. unsigned idle_diff = idle - last_idle;
  50. last_busy = busy;
  51. last_idle = idle;
  52. float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
  53. cpu_history.enqueue(cpu);
  54. GPainter painter(*bitmap);
  55. painter.fill_rect(applet_rect, Color::Black);
  56. int i = cpu_history.capacity() - cpu_history.size();
  57. for (auto cpu_usage : cpu_history) {
  58. painter.draw_line(
  59. { applet_rect.x() + i, applet_rect.bottom() },
  60. { applet_rect.x() + i, (int)(applet_rect.y() + (applet_rect.height() - (cpu_usage * (float)applet_rect.height()))) },
  61. Color::from_rgb(0xaa6d4b));
  62. ++i;
  63. }
  64. GWindowServerConnection::the().send_sync<WindowServer::InvalidateMenuAppletRect>(applet_id, applet_rect);
  65. };
  66. repaint();
  67. auto timer = CTimer::construct(1000, [&] {
  68. repaint();
  69. });
  70. return app.exec();
  71. }