|
| 1 | +#include "runtime.h" |
| 2 | + |
| 3 | +#include <stddef.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <string.h> |
| 6 | +#include <time.h> |
| 7 | + |
| 8 | +#include "../../common/dictionary.h" |
| 9 | + |
| 10 | +#ifndef INTERFACE_COUNTER_PATH |
| 11 | +#define INTERFACE_COUNTER_PATH "/sdcard/interface-counter" |
| 12 | +#endif // INTERFACE_COUNTER_PATH |
| 13 | + |
| 14 | +#ifndef FILENAME_CAPACITY |
| 15 | +#define FILENAME_CAPACITY 128 |
| 16 | +#endif // FILENAME_CAPACITY |
| 17 | + |
| 18 | +#ifndef SAVE_INTERVAL |
| 19 | +#define SAVE_INTERVAL 8 |
| 20 | +#endif // SAVE_INTERVAL |
| 21 | + |
| 22 | +#ifdef ANDROID |
| 23 | +#include <android/log.h> |
| 24 | +#define LOG_TAG "liblaf" |
| 25 | +#define ALOGV(...) \ |
| 26 | + __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__) |
| 27 | +#define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) |
| 28 | +#define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) |
| 29 | +#define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) |
| 30 | +#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) |
| 31 | +#define ALOGF(...) __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, __VA_ARGS__) |
| 32 | +#else |
| 33 | +#define ALOGV(...) |
| 34 | +#define ALOGD(...) |
| 35 | +#define ALOGI(...) |
| 36 | +#define ALOGW(...) |
| 37 | +#define ALOGE(...) |
| 38 | +#define ALOGF(...) |
| 39 | +#endif |
| 40 | + |
| 41 | +DEFINE_DICTIONARY(Dict, const char*, int); |
| 42 | + |
| 43 | +static void save_to_file(); |
| 44 | +static void __attribute__((constructor(100))) runtime_init(); |
| 45 | +static void __attribute__((destructor(100))) runtime_exit(); |
| 46 | + |
| 47 | +static FILE* file = NULL; |
| 48 | +static struct Dict* dict = NULL; |
| 49 | +static int counter = 0; |
| 50 | + |
| 51 | +int Dict_cmp(const DictKeyType* a, const DictKeyType* b) { |
| 52 | + return strcmp(*a, *b); |
| 53 | +} |
| 54 | + |
| 55 | +int Dict_hash(const DictKeyType* key) { |
| 56 | + int hash = 0; |
| 57 | + for (size_t n = strlen(*key), i = 0; i < n; i++) { |
| 58 | + hash = (hash << 5) | (hash >> 27); |
| 59 | + hash += (*key)[i]; |
| 60 | + hash &= 0x7fffffff; |
| 61 | + } |
| 62 | + return hash; |
| 63 | +} |
| 64 | + |
| 65 | +void Dict_key_copy(DictKeyType* dest, const DictKeyType* src) { *dest = *src; } |
| 66 | + |
| 67 | +void Dict_value_copy(DictValueType* dest, const DictValueType* src) { |
| 68 | + *dest = *src; |
| 69 | +} |
| 70 | + |
| 71 | +void __before_interface_call(const char* loc, const char* called_func_name) { |
| 72 | + int* value = Dict_get(dict, called_func_name); |
| 73 | + if (value) { |
| 74 | + ++(*value); |
| 75 | + } else { |
| 76 | + Dict_put(dict, called_func_name, 1); |
| 77 | + } |
| 78 | + if (++counter >= SAVE_INTERVAL) { |
| 79 | + save_to_file(); |
| 80 | + counter = 0; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +static void save_to_file() { |
| 85 | + rewind(file); |
| 86 | + for (int i = 0; i < Dict_capacity(dict); ++i) { |
| 87 | + if (dict->_buckets[i]) { |
| 88 | + fprintf(file, "%s %d\n", dict->_buckets[i]->key, |
| 89 | + dict->_buckets[i]->value); |
| 90 | + } |
| 91 | + } |
| 92 | + fflush(file); |
| 93 | +} |
| 94 | + |
| 95 | +static void runtime_init() { |
| 96 | + if (dict || file) return; |
| 97 | + dict = Dict_new(1000009); |
| 98 | + char filename[FILENAME_CAPACITY]; |
| 99 | + sprintf(filename, "%s/%09ld.txt", INTERFACE_COUNTER_PATH, clock()); |
| 100 | + file = fopen(filename, "w"); |
| 101 | + ALOGI("runtime_init(%s)", filename); |
| 102 | +} |
| 103 | + |
| 104 | +static void runtime_exit() { |
| 105 | + ALOGI("runtime_exit()"); |
| 106 | + save_to_file(); |
| 107 | + fclose(file); |
| 108 | + Dict_delete(dict); |
| 109 | + dict = NULL; |
| 110 | +} |
0 commit comments