• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "metrics_receiver.h"
17 
18 #include <android-base/logging.h>
19 #include <android-base/strings.h>
20 #include <gflags/gflags.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/ipc.h>
26 #include <sys/msg.h>
27 #include <fstream>
28 #include <iostream>
29 #include <memory>
30 
31 #include "common/libs/utils/tee_logging.h"
32 #include "host/commands/metrics/metrics_defs.h"
33 #include "host/libs/config/cuttlefish_config.h"
34 #include "host/libs/msg_queue/msg_queue.h"
35 
36 using cuttlefish::MetricsExitCodes;
37 
38 namespace cuttlefish {
39 
MetricsReceiver()40 MetricsReceiver::MetricsReceiver() {}
41 
~MetricsReceiver()42 MetricsReceiver::~MetricsReceiver() {}
43 
SendHelper(const std::string & message)44 void MetricsReceiver::SendHelper(const std::string &message) {
45   auto msg_queue = SysVMessageQueue::Create("cuttlefish_ipc", 'a', false);
46   if (msg_queue == NULL) {
47     LOG(FATAL) << "Create: failed to create cuttlefish_ipc";
48   }
49 
50   struct msg_buffer msg;
51   msg.mesg_type = 1;
52   strcpy(msg.mesg_text, message.c_str());
53   int rc = msg_queue->Send(&msg, message.length() + 1, true);
54   if (rc == -1) {
55     LOG(FATAL) << "Send: failed to send message to msg_queue";
56   }
57 }
58 
LogMetricsVMStart()59 void MetricsReceiver::LogMetricsVMStart() { SendHelper("VMStart"); }
60 
LogMetricsVMStop()61 void MetricsReceiver::LogMetricsVMStop() { SendHelper("VMStop"); }
62 
LogMetricsDeviceBoot()63 void MetricsReceiver::LogMetricsDeviceBoot() { SendHelper("DeviceBoot"); }
64 
LogMetricsLockScreen()65 void MetricsReceiver::LogMetricsLockScreen() { SendHelper("LockScreen"); }
66 }  // namespace cuttlefish
67