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