blob: b7c5296001ef6ab88a52069e3f4d51582a2b0aa1 [file] [log] [blame]
Ray Essick6ce27e52019-02-15 10:58:05 -08001/*
2 * Copyright (C) 2019 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
17//#define LOG_NDEBUG 0
18#define LOG_TAG "iface_statsd"
19#include <utils/Log.h>
20
21#include <stdint.h>
22#include <inttypes.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <dirent.h>
27#include <pthread.h>
28#include <unistd.h>
29
Robert Shih2e15aed2021-03-16 18:30:35 -070030#include <map>
Andy Hung82a074b2019-12-03 14:16:45 -080031#include <memory>
Robert Shih2e15aed2021-03-16 18:30:35 -070032#include <string>
33#include <vector>
Ray Essick6ce27e52019-02-15 10:58:05 -080034#include <string.h>
35#include <pwd.h>
36
Ray Essick40e8e5e2019-12-05 20:19:40 -080037#include "MediaMetricsService.h"
Ray Essick6ce27e52019-02-15 10:58:05 -080038#include "iface_statsd.h"
39
40#include <statslog.h>
41
42namespace android {
43
Ray Essickf27e9872019-12-07 06:28:46 -080044// set of routines that crack a mediametrics::Item
Ray Essick6ce27e52019-02-15 10:58:05 -080045// and send it off to statsd with the appropriate hooks
46//
Ray Essickf27e9872019-12-07 06:28:46 -080047// each mediametrics::Item type (extractor, codec, nuplayer, etc)
Ray Essick6ce27e52019-02-15 10:58:05 -080048// has its own routine to handle this.
49//
50
51bool enabled_statsd = true;
52
Robert Shih2e15aed2021-03-16 18:30:35 -070053using statsd_pusher = bool (*)(const mediametrics::Item *);
54using statsd_puller = bool (*)(const mediametrics::Item *, AStatsEventList *);
Ray Essick6ce27e52019-02-15 10:58:05 -080055
Robert Shih2e15aed2021-03-16 18:30:35 -070056namespace {
57template<typename Handler, typename... Args>
58bool dump2StatsdInternal(const std::map<std::string, Handler>& handlers,
59 const std::shared_ptr<const mediametrics::Item>& item, Args... args) {
Andy Hung3ab1b322020-05-18 10:47:31 -070060 if (item == nullptr) return false;
Ray Essick6ce27e52019-02-15 10:58:05 -080061
62 // get the key
63 std::string key = item->getKey();
64
65 if (!enabled_statsd) {
66 ALOGV("statsd logging disabled for record key=%s", key.c_str());
67 return false;
68 }
69
Robert Shih2e15aed2021-03-16 18:30:35 -070070 if (handlers.count(key)) {
71 return (handlers.at(key))(item.get(), args...);
Ray Essick6ce27e52019-02-15 10:58:05 -080072 }
73 return false;
74}
Robert Shih2e15aed2021-03-16 18:30:35 -070075} // namespace
76
77// give me a record, I'll look at the type and upload appropriately
78bool dump2Statsd(const std::shared_ptr<const mediametrics::Item>& item) {
79 static const std::map<std::string, statsd_pusher> statsd_pushers =
80 {
81 { "audiopolicy", statsd_audiopolicy },
82 { "audiorecord", statsd_audiorecord },
83 { "audiothread", statsd_audiothread },
84 { "audiotrack", statsd_audiotrack },
85 { "codec", statsd_codec},
86 { "drmmanager", statsd_drmmanager },
87 { "extractor", statsd_extractor },
88 { "mediadrm", statsd_mediadrm },
89 { "mediaparser", statsd_mediaparser },
90 { "nuplayer", statsd_nuplayer },
91 { "nuplayer2", statsd_nuplayer },
92 { "recorder", statsd_recorder },
93 };
94 return dump2StatsdInternal(statsd_pushers, item);
95}
96
97bool dump2Statsd(const std::shared_ptr<const mediametrics::Item>& item, AStatsEventList* out) {
98 static const std::map<std::string, statsd_puller> statsd_pullers =
99 {
100 { "mediadrm", statsd_mediadrm_puller },
101 };
102 return dump2StatsdInternal(statsd_pullers, item, out);
103}
Ray Essick6ce27e52019-02-15 10:58:05 -0800104
105} // namespace android