1 // 2 // Copyright (C) 2022 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 #include <android-base/logging.h> 17 18 #include "host/commands/metrics/events.h" 19 #include "host/commands/metrics/host_receiver.h" 20 #include "host/commands/metrics/metrics_configs.h" 21 #include "host/commands/metrics/metrics_defs.h" 22 #include "host/commands/metrics/proto/cf_metrics_protos.h" 23 #include "host/libs/metrics/metrics_receiver.h" 24 #include "host/libs/msg_queue/msg_queue.h" 25 26 namespace cuttlefish { 27 MetricsHostReceiver(bool is_metrics_enabled)28MetricsHostReceiver::MetricsHostReceiver(bool is_metrics_enabled) 29 : is_metrics_enabled_(is_metrics_enabled) {} 30 ~MetricsHostReceiver()31MetricsHostReceiver::~MetricsHostReceiver() {} 32 ServerLoop()33void MetricsHostReceiver::ServerLoop() { 34 auto msg_queue = SysVMessageQueue::Create(metrics_queue_name_); 35 if (msg_queue == NULL) { 36 LOG(FATAL) << "create: failed to create" << metrics_queue_name_; 37 } 38 39 struct msg_buffer msg = {0, {0}}; 40 while (true) { 41 int rc = msg_queue->Receive(&msg, MAX_MSG_SIZE, 1, true); 42 if (rc == -1) { 43 LOG(FATAL) << "receive: failed to receive any messages"; 44 } 45 46 std::string text(msg.mesg_text); 47 LOG(INFO) << "Metrics host received: " << text; 48 49 // Process the received message 50 ProcessMessage(text); 51 52 sleep(1); 53 } 54 } 55 Join()56void MetricsHostReceiver::Join() { thread_.join(); } 57 Initialize(const std::string & metrics_queue_name)58bool MetricsHostReceiver::Initialize(const std::string& metrics_queue_name) { 59 metrics_queue_name_ = metrics_queue_name; 60 if (!is_metrics_enabled_) { 61 LOG(ERROR) << "init: metrics not enabled"; 62 return false; 63 } 64 65 // Start the server loop in a separate thread 66 thread_ = std::thread(&MetricsHostReceiver::ServerLoop, this); 67 return true; 68 } 69 ProcessMessage(const std::string & text)70void MetricsHostReceiver::ProcessMessage(const std::string& text) { 71 auto hostDev = CuttlefishLogEvent::CUTTLEFISH_DEVICE_TYPE_HOST; 72 73 int rc = MetricsExitCodes::kSuccess; 74 75 if (text == "VMStart") { 76 rc = Clearcut::SendVMStart(hostDev); 77 } else if (text == "VMStop") { 78 rc = Clearcut::SendVMStop(hostDev); 79 } else if (text == "DeviceBoot") { 80 rc = Clearcut::SendDeviceBoot(hostDev); 81 } else if (text == "LockScreen") { 82 rc = Clearcut::SendLockScreen(hostDev); 83 } else { 84 LOG(ERROR) << "Message not recognized: " << text; 85 rc = MetricsExitCodes::kMetricsError; 86 } 87 88 if (rc != MetricsExitCodes::kSuccess) { 89 LOG(ERROR) << "Message failed to send to ClearCut: " << text; 90 } 91 } 92 93 } // namespace cuttlefish 94