1 /*
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
30 #include <map>
31 #include <memory>
32 #include <string>
33 #include <vector>
34 #include <string.h>
35 #include <pwd.h>
36
37 #include "MediaMetricsService.h"
38 #include "iface_statsd.h"
39
40 #include <statslog.h>
41
42 namespace android {
43
44 // set of routines that crack a mediametrics::Item
45 // and send it off to statsd with the appropriate hooks
46 //
47 // each mediametrics::Item type (extractor, codec, nuplayer, etc)
48 // has its own routine to handle this.
49 //
50
51 static bool enabled_statsd = true;
52
53 namespace {
54 template<typename Handler, typename... Args>
dump2StatsdInternal(const std::map<std::string,Handler> & handlers,const std::shared_ptr<const mediametrics::Item> & item,Args...args)55 bool dump2StatsdInternal(const std::map<std::string, Handler>& handlers,
56 const std::shared_ptr<const mediametrics::Item>& item, Args... args) {
57 if (item == nullptr) return false;
58
59 // get the key
60 std::string key = item->getKey();
61
62 if (!enabled_statsd) {
63 ALOGV("statsd logging disabled for record key=%s", key.c_str());
64 return false;
65 }
66
67 if (handlers.count(key)) {
68 return (handlers.at(key))(item, args...);
69 }
70 return false;
71 }
72 } // namespace
73
74 // give me a record, I'll look at the type and upload appropriately
dump2Statsd(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)75 bool dump2Statsd(
76 const std::shared_ptr<const mediametrics::Item>& item,
77 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog) {
78 static const std::map<std::string, statsd_pusher*> statsd_pushers =
79 {
80 { "audiopolicy", statsd_audiopolicy },
81 { "audiorecord", statsd_audiorecord },
82 { "audiothread", statsd_audiothread },
83 { "audiotrack", statsd_audiotrack },
84 { "codec", statsd_codec},
85 { "drmmanager", statsd_drmmanager },
86 { "extractor", statsd_extractor },
87 { "mediadrm", statsd_mediadrm },
88 { "mediaparser", statsd_mediaparser },
89 { "nuplayer", statsd_nuplayer },
90 { "nuplayer2", statsd_nuplayer },
91 { "recorder", statsd_recorder },
92 };
93 return dump2StatsdInternal(statsd_pushers, item, statsdLog);
94 }
95
dump2Statsd(const std::shared_ptr<const mediametrics::Item> & item,AStatsEventList * out,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)96 bool dump2Statsd(const std::shared_ptr<const mediametrics::Item>& item, AStatsEventList* out,
97 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog) {
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, statsdLog);
103 }
104
105 } // namespace android
106