CCF
Loading...
Searching...
No Matches
load_monitor.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ds/messaging.h"
6#include "timer.h"
7
8namespace asynchost
9{
11 {
12 using TClock = std::chrono::system_clock;
13 std::chrono::milliseconds last_update;
14
16
17 nlohmann::json enclave_counts;
18
19 public:
21 dispatcher(bp.get_dispatcher())
22 {
23 dispatcher.retrieve_message_counts();
24 last_update = std::chrono::duration_cast<std::chrono::milliseconds>(
25 TClock::now().time_since_epoch());
26
27 enclave_counts = nlohmann::json::object();
28
29 // Register message handler for work_stats message from enclave
31 bp, AdminMessage::work_stats, [this](const uint8_t* data, size_t size) {
32 auto [dumped_json] =
33 ringbuffer::read_message<AdminMessage::work_stats>(data, size);
34
35 nlohmann::json j;
36 try
37 {
38 j = nlohmann::json::parse(dumped_json);
39 }
40 catch (const nlohmann::json::parse_error& e)
41 {
42 LOG_FAIL_FMT("Received unparseable work_stats from enclave");
43 return;
44 }
45
46 for (const auto& [outer_key, outer_value] : j.items())
47 {
48 for (const auto& [inner_key, inner_value] : outer_value.items())
49 {
50 auto& outer_obj = enclave_counts[outer_key];
51 auto it = outer_obj.find(inner_key);
52 if (it == outer_obj.end())
53 {
54 outer_obj[inner_key] = inner_value;
55 }
56 else
57 {
58 const auto prev = it.value().get<size_t>();
59 outer_obj[inner_key] = prev + inner_value.get<size_t>();
60 }
61 }
62 }
63 });
64 }
65
66 void on_timer()
67 {
68 const auto message_counts = dispatcher.retrieve_message_counts();
69 const auto time_now =
70 std::chrono::duration_cast<std::chrono::milliseconds>(
71 TClock::now().time_since_epoch());
72
73 if (!message_counts.empty())
74 {
75 auto j = nlohmann::json::object();
76
77 j["start_time_ms"] = last_update.count();
78 j["end_time_ms"] = time_now.count();
79
80 {
81 j["ringbuffer_messages"] =
82 dispatcher.convert_message_counts(message_counts);
83
84 LOG_DEBUG_FMT("Host load: {}", j.dump());
85 }
86
87 {
88 j["ringbuffer_messages"] = enclave_counts;
89 enclave_counts = nlohmann::json::object();
90
91 LOG_DEBUG_FMT("Enclave load: {}", j.dump());
92 }
93
94 last_update = time_now;
95 }
96 }
97 };
98
100}
Definition load_monitor.h:11
LoadMonitorImpl(messaging::BufferProcessor &bp)
Definition load_monitor.h:20
void on_timer()
Definition load_monitor.h:66
Definition proxy.h:51
Definition messaging.h:211
Definition messaging.h:38
nlohmann::json convert_message_counts(const MessageCounts &mc)
Definition messaging.h:190
MessageCounts retrieve_message_counts()
Definition messaging.h:183
#define LOG_DEBUG_FMT
Definition logger.h:357
#define LOG_FAIL_FMT
Definition logger.h:363
#define DISPATCHER_SET_MESSAGE_HANDLER(DISP, MSG,...)
Definition messaging.h:316
Definition after_io.h:8