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_configs.h"
33 #include "host/commands/metrics/metrics_defs.h"
34 #include "host/libs/config/cuttlefish_config.h"
35 #include "host/libs/msg_queue/msg_queue.h"
36
37 using cuttlefish::MetricsExitCodes;
38
39 namespace cuttlefish {
40
SendHelper(const std::string & queue_name,const std::string & message)41 void SendHelper(const std::string &queue_name, const std::string &message) {
42 auto msg_queue = SysVMessageQueue::Create(queue_name, false);
43 if (msg_queue == NULL) {
44 LOG(FATAL) << "Create: failed to create" << queue_name;
45 }
46
47 struct msg_buffer msg;
48 msg.mesg_type = 1;
49 strcpy(msg.mesg_text, message.c_str());
50 int rc = msg_queue->Send(&msg, message.length() + 1, true);
51 if (rc == -1) {
52 LOG(FATAL) << "Send: failed to send message to msg_queue";
53 }
54 }
55
LogMetricsVMStart()56 void MetricsReceiver::LogMetricsVMStart() {
57 SendHelper(kCfMetricsQueueName, "VMStart");
58 }
59
LogMetricsVMStop()60 void MetricsReceiver::LogMetricsVMStop() {
61 SendHelper(kCfMetricsQueueName, "VMStop");
62 }
63
LogMetricsDeviceBoot()64 void MetricsReceiver::LogMetricsDeviceBoot() {
65 SendHelper(kCfMetricsQueueName, "DeviceBoot");
66 }
67
LogMetricsLockScreen()68 void MetricsReceiver::LogMetricsLockScreen() {
69 SendHelper(kCfMetricsQueueName, "LockScreen");
70 }
71
72 } // namespace cuttlefish
73