Add high-resolution timekeeping functions (primarily for benchmarking).
[bluesky.git] / bluesky / util.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <glib.h>
12 #include <string.h>
13
14 #include "bluesky.h"
15
16 /* Miscellaneous useful functions that don't really fit anywhere else. */
17
18 bluesky_time_hires bluesky_now_hires()
19 {
20     struct timespec time;
21
22     if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
23         perror("clock_gettime");
24         return 0;
25     }
26
27     return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
28 }
29
30 /* Convert a UTF-8 string to lowercase.  This can be used to implement
31  * case-insensitive lookups and comparisons, by normalizing all values to
32  * lowercase first.  Returns a newly-allocated string as a result. */
33 gchar *bluesky_lowercase(const gchar *s)
34 {
35     /* TODO: Unicode handling; for now just do ASCII. */
36     return g_ascii_strdown(s, -1);
37 }