diff options
author | Mark Salyzyn | 2015-10-02 11:22:52 -0500 |
---|---|---|
committer | Mark Salyzyn | 2015-10-02 18:45:22 -0500 |
commit | ddda212faa81d62f637926680cd8163345120f71 (patch) | |
tree | 73f729e24adb87f184691d0014cf377b398a6a64 /logd/LogAudit.cpp | |
parent | 5ac5c6b19364b5b3061a59db796b2357c95c3b64 (diff) | |
download | platform-system-core-ddda212faa81d62f637926680cd8163345120f71.tar.gz platform-system-core-ddda212faa81d62f637926680cd8163345120f71.tar.xz platform-system-core-ddda212faa81d62f637926680cd8163345120f71.zip |
logd: optimize code hotspots
Discovered that we had a few libc hotspots. Adjust code to generally
reduce or nullify the number of calls to malloc, free, strlen,
strcmp, strncmp, memcmp & strncasecmp. Total gain looks to be about
3% of logd's processing time. malloc still contributes to 3%, but all
others are now total 0.5%.
Bug: 23685592
Change-Id: Ife721121667969260cdb8b055524ae90f5911278
Diffstat (limited to 'logd/LogAudit.cpp')
-rw-r--r-- | logd/LogAudit.cpp | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/logd/LogAudit.cpp b/logd/LogAudit.cpp index 7db17d1df..808d17a66 100644 --- a/logd/LogAudit.cpp +++ b/logd/LogAudit.cpp | |||
@@ -24,6 +24,7 @@ | |||
24 | #include <sys/uio.h> | 24 | #include <sys/uio.h> |
25 | #include <syslog.h> | 25 | #include <syslog.h> |
26 | 26 | ||
27 | #include <log/logger.h> | ||
27 | #include <private/android_filesystem_config.h> | 28 | #include <private/android_filesystem_config.h> |
28 | #include <private/android_logger.h> | 29 | #include <private/android_logger.h> |
29 | 30 | ||
@@ -153,15 +154,16 @@ int LogAudit::logPrint(const char *fmt, ...) { | |||
153 | 154 | ||
154 | // log to events | 155 | // log to events |
155 | 156 | ||
156 | size_t l = strlen(str); | 157 | size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD); |
157 | size_t n = l + sizeof(android_log_event_string_t); | 158 | size_t n = l + sizeof(android_log_event_string_t); |
158 | 159 | ||
159 | bool notify = false; | 160 | bool notify = false; |
160 | 161 | ||
161 | android_log_event_string_t *event = static_cast<android_log_event_string_t *>(malloc(n)); | 162 | { // begin scope for event buffer |
162 | if (!event) { | 163 | uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; |
163 | rc = -ENOMEM; | 164 | |
164 | } else { | 165 | android_log_event_string_t *event |
166 | = reinterpret_cast<android_log_event_string_t *>(buffer); | ||
165 | event->header.tag = htole32(AUDITD_LOG_TAG); | 167 | event->header.tag = htole32(AUDITD_LOG_TAG); |
166 | event->type = EVENT_TYPE_STRING; | 168 | event->type = EVENT_TYPE_STRING; |
167 | event->length = htole32(l); | 169 | event->length = htole32(l); |
@@ -170,11 +172,10 @@ int LogAudit::logPrint(const char *fmt, ...) { | |||
170 | rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid, | 172 | rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid, |
171 | reinterpret_cast<char *>(event), | 173 | reinterpret_cast<char *>(event), |
172 | (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX); | 174 | (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX); |
173 | free(event); | ||
174 | |||
175 | if (rc >= 0) { | 175 | if (rc >= 0) { |
176 | notify = true; | 176 | notify = true; |
177 | } | 177 | } |
178 | // end scope for event buffer | ||
178 | } | 179 | } |
179 | 180 | ||
180 | // log to main | 181 | // log to main |
@@ -206,24 +207,28 @@ int LogAudit::logPrint(const char *fmt, ...) { | |||
206 | l = strlen(comm) + 1; | 207 | l = strlen(comm) + 1; |
207 | ecomm = ""; | 208 | ecomm = ""; |
208 | } | 209 | } |
209 | n = (estr - str) + strlen(ecomm) + l + 2; | 210 | size_t b = estr - str; |
211 | if (b > LOGGER_ENTRY_MAX_PAYLOAD) { | ||
212 | b = LOGGER_ENTRY_MAX_PAYLOAD; | ||
213 | } | ||
214 | size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b); | ||
215 | n = b + e + l + 2; | ||
216 | |||
217 | { // begin scope for main buffer | ||
218 | char newstr[n]; | ||
210 | 219 | ||
211 | char *newstr = static_cast<char *>(malloc(n)); | ||
212 | if (!newstr) { | ||
213 | rc = -ENOMEM; | ||
214 | } else { | ||
215 | *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN; | 220 | *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN; |
216 | strlcpy(newstr + 1, comm, l); | 221 | strlcpy(newstr + 1, comm, l); |
217 | strncpy(newstr + 1 + l, str, estr - str); | 222 | strncpy(newstr + 1 + l, str, b); |
218 | strcpy(newstr + 1 + l + (estr - str), ecomm); | 223 | strncpy(newstr + 1 + l + b, ecomm, e); |
219 | 224 | ||
220 | rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr, | 225 | rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr, |
221 | (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX); | 226 | (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX); |
222 | free(newstr); | ||
223 | 227 | ||
224 | if (rc >= 0) { | 228 | if (rc >= 0) { |
225 | notify = true; | 229 | notify = true; |
226 | } | 230 | } |
231 | // end scope for main buffer | ||
227 | } | 232 | } |
228 | 233 | ||
229 | free(commfree); | 234 | free(commfree); |