1 /*
2 * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3 */
4 #include "dev_bind_internal.h"
5
6 #ifdef AWSS_SUPPORT_DEV_BIND_STATIS
7 static struct awss_statis_dev_bind_t g_db_statis = { 0 };
8 static uint32_t awss_statis_db_report_id = 0;
9 static uint32_t awss_statis_db_trace_id = 0;
10 static void *awss_statis_db_mutex = NULL;
11
12 #define DB_CNT g_db_statis.dev_bind_cnt
13 #define DB_SUC g_db_statis.dev_bind_suc
14 #define DB_TMEAN g_db_statis.dev_bind_time_mean
15 #define DB_TMIN g_db_statis.dev_bind_time_min
16 #define DB_TMAX g_db_statis.dev_bind_time_max
17 #define DB_START g_db_statis.dev_bind_start
18 #define DB_END g_db_statis.dev_bind_end
19
20 #define AWSS_STATIS_DB_BUF_LEN (512)
21
awss_bind_report_statis(const char * module)22 int awss_bind_report_statis(const char *module)
23 {
24 const char *elem_fmt = "[%s max:%u min:%u mean:%u cnt:%u suc:%u]";
25 int log_buf_len = AWSS_STATIS_DB_BUF_LEN + strlen(AWSS_STATIS_FMT) + 21;
26 char statis_topic[TOPIC_LEN_MAX] = { 0 };
27 char *log_content = NULL;
28 char id_str[21] = { 0 };
29 char *log_buf = NULL;
30 int len = 0;
31 int ret;
32
33 log_content = awss_zalloc(AWSS_STATIS_DB_BUF_LEN + 1);
34 if (log_content == NULL) {
35 goto BIND_STATIS_ERR;
36 }
37 log_buf = awss_zalloc(log_buf_len + 1);
38 if (log_buf == NULL) {
39 goto BIND_STATIS_ERR;
40 }
41
42 if (awss_build_topic(TOPIC_POST_STATIS, statis_topic, TOPIC_LEN_MAX) ==
43 NULL) {
44 awss_err("awss build statis topic fail\n");
45 goto BIND_STATIS_ERR;
46 }
47
48 if (awss_statis_db_mutex) {
49 HAL_MutexLock(awss_statis_db_mutex);
50 }
51 do {
52 if (DB_CNT == 0) {
53 break;
54 }
55
56 len += HAL_Snprintf(log_buf + len, log_buf_len - len, elem_fmt,
57 "SyncToken", DB_TMAX, DB_TMIN, DB_TMEAN, DB_CNT,
58 DB_SUC);
59
60 HAL_Snprintf(log_content, AWSS_STATIS_DB_BUF_LEN, AWSS_STATIS_FMT,
61 (uint32_t)HAL_UptimeMs(), "BIND_TRACE",
62 module == NULL ? "default" : module,
63 awss_statis_db_trace_id, log_buf);
64
65 HAL_Snprintf(id_str, sizeof(id_str), "%u", ++awss_statis_db_report_id);
66
67 awss_build_packet(AWSS_CMP_PKT_TYPE_REQ, id_str, ILOP_VER,
68 METHOD_LOG_POST, log_content, 0, log_buf,
69 &log_buf_len);
70
71 awss_debug("%s\n", log_buf);
72
73 ret = awss_cmp_mqtt_send(statis_topic, log_buf, strlen(log_buf), 0);
74
75 awss_info("bind report statis %s\n", ret == 0 ? "success" : "fail");
76 } while (0);
77
78 if (awss_statis_db_mutex) {
79 HAL_MutexUnlock(awss_statis_db_mutex);
80 }
81
82 HAL_Free(log_buf);
83 HAL_Free(log_content);
84
85 return 0;
86
87 BIND_STATIS_ERR:
88 if (log_content) {
89 HAL_Free(log_content);
90 }
91 if (log_buf) {
92 HAL_Free(log_buf);
93 }
94 return -1;
95 }
96
awss_bind_clear_statis()97 void awss_bind_clear_statis()
98 {
99 if (awss_statis_db_mutex) {
100 HAL_MutexLock(awss_statis_db_mutex);
101 }
102
103 memset(&g_db_statis, 0, sizeof(g_db_statis));
104
105 awss_statis_db_trace_id = 0;
106 awss_statis_db_report_id = 0;
107
108 if (awss_statis_db_mutex) {
109 HAL_MutexUnlock(awss_statis_db_mutex);
110 HAL_MutexDestroy(awss_statis_db_mutex);
111 }
112 awss_statis_db_mutex = NULL;
113 }
114
awss_bind_update_statis(int type)115 void awss_bind_update_statis(int type)
116 {
117 uint32_t time = HAL_UptimeMs();
118
119 if (awss_statis_db_mutex == NULL) {
120 awss_statis_db_mutex = HAL_MutexCreate();
121 if (awss_statis_db_mutex == NULL) {
122 awss_debug("a-statis am fail\n");
123 return;
124 }
125 }
126
127 HAL_MutexLock(awss_statis_db_mutex);
128
129 if (type == AWSS_DB_STATIS_START) {
130 awss_statis_db_trace_id++;
131 }
132
133 switch (type) {
134 case AWSS_DB_STATIS_START:
135 DB_CNT++;
136 DB_START = time;
137 break;
138 case AWSS_DB_STATIS_SUC:
139 DB_END = time;
140 DB_SUC++;
141 time = (uint32_t)(DB_END - DB_START);
142 if (DB_SUC > 0) {
143 DB_TMEAN = (DB_TMEAN + time) / DB_SUC;
144 } else {
145 DB_SUC = 1;
146 DB_TMEAN = time;
147 }
148 if (DB_TMIN == 0 || DB_TMIN > time) {
149 DB_TMIN = time;
150 }
151 if (DB_TMAX == 0 || DB_TMAX < time) {
152 DB_TMAX = time;
153 }
154 break;
155 default:
156 break;
157 }
158 HAL_MutexUnlock(awss_statis_db_mutex);
159 }
160
awss_bind_disp_statis()161 void awss_bind_disp_statis()
162 {
163 if (awss_statis_db_mutex) {
164 HAL_MutexLock(awss_statis_db_mutex);
165 }
166
167 awss_debug(
168 "--------------------------DEV BIND "
169 "STATIS-----------------------------");
170 awss_debug("name\t\tmax\tmin\tmean\tcnt\tsuc");
171 awss_debug("SyncToken \t%u\t%u\t%u\t%u\t%u\t", DB_TMAX, DB_TMIN,
172 DB_TMEAN, DB_CNT, DB_SUC);
173 awss_debug(
174 "---------------------------------------------------------------------"
175 "-");
176
177 if (awss_statis_db_mutex) {
178 HAL_MutexUnlock(awss_statis_db_mutex);
179 }
180 }
181 #endif
182