diff options
author | koekeishiya <aasvi93@hotmail.com> | 2019-02-24 13:28:59 +0100 |
---|---|---|
committer | koekeishiya <aasvi93@hotmail.com> | 2019-02-24 13:28:59 +0100 |
commit | 600cf250cf6e00efb71117070b7b28074997e8cb (patch) | |
tree | bb7248acd6efb202653c6fe3d219cbb652516276 /src | |
parent | 8a952517b0ec62eb87e8053e26eb845a21b3978c (diff) | |
download | skhd-600cf250cf6e00efb71117070b7b28074997e8cb.tar.gz skhd-600cf250cf6e00efb71117070b7b28074997e8cb.zip |
#54 more accurate profiling information
Diffstat (limited to '')
-rw-r--r-- | src/skhd.c | 37 | ||||
-rw-r--r-- | src/timing.h | 70 |
2 files changed, 79 insertions, 28 deletions
@@ -8,6 +8,7 @@ #include <unistd.h> #include <Carbon/Carbon.h> +#include "timing.h" #include "log.h" #define HASHTABLE_IMPLEMENTATION #include "hashtable.h" @@ -34,30 +35,6 @@ extern bool CGSIsSecureEventInputSet(); #define secure_keyboard_entry_enabled CGSIsSecureEventInputSet -#ifdef SKHD_PROFILE -#define BEGIN_SCOPED_TIMED_BLOCK(note) \ - do { \ - char *timed_note = note; \ - clock_t timed_block_begin = clock() -#define END_SCOPED_TIMED_BLOCK() \ - clock_t timed_block_end = clock(); \ - double timed_block_elapsed = ((timed_block_end - timed_block_begin) / (double)CLOCKS_PER_SEC) * 1000.0f; \ - printf("%.4fms (%s)\n", timed_block_elapsed, timed_note); \ - } while (0) -#define BEGIN_TIMED_BLOCK(note) \ - char *timed_note = note; \ - clock_t timed_block_begin = clock() -#define END_TIMED_BLOCK() \ - clock_t timed_block_end = clock(); \ - double timed_block_elapsed = ((timed_block_end - timed_block_begin) / (double)CLOCKS_PER_SEC) * 1000.0f; \ - printf("%.4fms (%s)\n", timed_block_elapsed, timed_note) -#else -#define BEGIN_SCOPED_TIMED_BLOCK(note) -#define END_SCOPED_TIMED_BLOCK() -#define BEGIN_TIMED_BLOCK(note) -#define END_TIMED_BLOCK() -#endif - #define SKHD_CONFIG_FILE ".skhdrc" internal unsigned major_version = 0; @@ -140,9 +117,10 @@ internal bool parse_arguments(int argc, char **argv) { int option; - const char *short_option = "Vvc:k:t:"; + const char *short_option = "VPvc:k:t:"; struct option long_option[] = { { "verbose", no_argument, NULL, 'V' }, + { "profile", no_argument, NULL, 'P' }, { "version", no_argument, NULL, 'v' }, { "config", required_argument, NULL, 'c' }, { "key", required_argument, NULL, 'k' }, @@ -155,6 +133,9 @@ parse_arguments(int argc, char **argv) case 'V': { verbose = true; } break; + case 'P': { + profile = true; + } break; case 'v': { printf("skhd version %d.%d.%d\n", major_version, minor_version, patch_version); return true; @@ -214,12 +195,12 @@ use_default_config_path(void) int main(int argc, char **argv) { - BEGIN_TIMED_BLOCK("startup"); - BEGIN_SCOPED_TIMED_BLOCK("initialization"); if (parse_arguments(argc, argv)) { return EXIT_SUCCESS; } + BEGIN_SCOPED_TIMED_BLOCK("total_time"); + BEGIN_SCOPED_TIMED_BLOCK("init"); if (getuid() == 0 || geteuid() == 0) { error("skhd: running as root is not allowed! abort..\n"); } @@ -274,7 +255,7 @@ int main(int argc, char **argv) warn("skhd: could not watch '%s'\n", config_file); } END_SCOPED_TIMED_BLOCK(); - END_TIMED_BLOCK(); + END_SCOPED_TIMED_BLOCK(); CFRunLoopRun(); return EXIT_SUCCESS; diff --git a/src/timing.h b/src/timing.h new file mode 100644 index 0000000..44e91a8 --- /dev/null +++ b/src/timing.h @@ -0,0 +1,70 @@ +#ifndef MACOS_TIMING_H +#define MACOS_TIMING_H + +#include <stdint.h> +#include <CoreAudio/CoreAudio.h> + +#define BEGIN_SCOPED_TIMED_BLOCK(note) \ + do { \ + struct timing_info timing; \ + if (profile) begin_timing(&timing, note) +#define END_SCOPED_TIMED_BLOCK() \ + if (profile) end_timing(&timing); \ + } while (0) + +#define BEGIN_TIMED_BLOCK(note) \ + struct timing_info timing; \ + if (profile) begin_timing(&timing, note) +#define END_TIMED_BLOCK() \ + if (profile) end_timing(&timing) + +static bool profile; + +struct timing_info +{ + char *note; + uint64_t start; + uint64_t end; + float ms; +}; + +void begin_timing(struct timing_info *timing, char *note); +void end_timing(struct timing_info *timing); + +static inline uint64_t +macos_get_wall_clock(void) +{ + uint64_t result = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()); + return result; +} + +static inline float +macos_get_seconds_elapsed(uint64_t start, uint64_t end) +{ + float result = ((float)(end - start) / 1000.0f) / 1000000.0f; + return result; +} + +static inline float +macos_get_milliseconds_elapsed(uint64_t start, uint64_t end) +{ + float result = 1000.0f * macos_get_seconds_elapsed(start, end); + return result; +} + +void begin_timing(struct timing_info *timing, char *note) { + timing->note = note; + timing->start = macos_get_wall_clock(); +} + +void end_timing(struct timing_info *timing) { + timing->end = macos_get_wall_clock(); + timing->ms = macos_get_milliseconds_elapsed(timing->start, timing->end); + if (timing->note) { + printf("%6.4fms (%s)\n", timing->ms, timing->note); + } else { + printf("%6.4fms\n", timing->ms); + } +} + +#endif |