uptimeofday.c 735 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // SPDX-License-Identifier: LGPL-2.1+
  2. // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
  3. #include <stdio.h>
  4. #include <sys/time.h>
  5. #include <linux/sysinfo.h>
  6. #include "thermal-tools.h"
  7. static unsigned long __offset;
  8. static struct timeval __tv;
  9. int uptimeofday_init(void)
  10. {
  11. struct sysinfo info;
  12. if (sysinfo(&info))
  13. return -1;
  14. gettimeofday(&__tv, NULL);
  15. __offset = __tv.tv_sec - info.uptime;
  16. return 0;
  17. }
  18. unsigned long getuptimeofday_ms(void)
  19. {
  20. gettimeofday(&__tv, NULL);
  21. return ((__tv.tv_sec - __offset) * 1000) + (__tv.tv_usec / 1000);
  22. }
  23. struct timespec msec_to_timespec(int msec)
  24. {
  25. struct timespec tv = {
  26. .tv_sec = (msec / 1000),
  27. .tv_nsec = (msec % 1000) * 1000000,
  28. };
  29. return tv;
  30. }