Changeset cc36562b in mainline


Ignore:
Timestamp:
2018-08-25T19:06:27Z (6 years ago)
Author:
Jakub Jermar <jakub@…>
Children:
72df613
Parents:
02f547f
Message:

Provide a single clock() implementation in libc

Also attempt to fix the implementation to return microseconds (clocks)
rather than processor cycles.

Location:
uspace/lib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/time.c

    r02f547f rcc36562b  
    5050#include <loc.h>
    5151#include <device/clock_dev.h>
     52#include <stats.h>
    5253
    5354#define ASCTIME_BUF_LEN  26
     
    7071static async_sess_t *clock_conn = NULL;
    7172
    72 /** Return processor time used by the program.
    73  *
    74  * @return -1  The processor time used is not available in this implementation.
     73/**
     74 * Get CPU time used since the process invocation.
     75 *
     76 * @return Consumed microseconds by this process or -1 if not available.
    7577 */
    7678clock_t clock(void)
    7779{
    78         return (clock_t) -1;
     80        static_assert(CLOCKS_PER_SEC == 1000000);
     81
     82        size_t count;
     83        stats_cpu_t *cpu_stats = stats_get_cpus(&count);
     84        if (!cpu_stats)
     85                return (clock_t) -1;
     86
     87        clock_t total_usecs = -1;
     88        if (cpu_stats) {
     89                stats_task_t *task_stats = stats_get_task(task_get_id());
     90                if (task_stats) {
     91                        total_usecs = (clock_t) (task_stats->kcycles +
     92                            task_stats->ucycles) / cpu_stats->frequency_mhz;
     93                        free(task_stats);
     94                }
     95                free(cpu_stats);
     96        }
     97
     98        return total_usecs;
    7999}
    80100
  • uspace/lib/c/include/time.h

    r02f547f rcc36562b  
    4545#include <_bits/NULL.h>
    4646
    47 #define CLOCKS_PER_SEC  ((clock_t) 1)
     47#define CLOCKS_PER_SEC  ((clock_t) 1000000)
    4848
    4949#define TIME_UTC        1
  • uspace/lib/posix/include/posix/time.h

    r02f547f rcc36562b  
    4242
    4343#include "libc/time.h"
    44 
    45 #undef CLOCKS_PER_SEC
    46 #define CLOCKS_PER_SEC (1000000L)
    4744
    4845#ifndef __locale_t_defined
     
    10299    const struct timespec *rqtp, struct timespec *rmtp);
    103100
    104 /* CPU Time */
    105 extern clock_t clock(void);
    106 
    107101#endif  // POSIX_TIME_H_
    108102
  • uspace/lib/posix/src/time.c

    r02f547f rcc36562b  
    4949#include "libc/malloc.h"
    5050#include "libc/task.h"
    51 #include "libc/stats.h"
    5251#include "libc/stddef.h"
    5352#include "libc/time.h"
     
    311310}
    312311
    313 /**
    314  * Get CPU time used since the process invocation.
    315  *
    316  * @return Consumed CPU cycles by this process or -1 if not available.
    317  */
    318 clock_t clock(void)
    319 {
    320         clock_t total_cycles = -1;
    321         stats_task_t *task_stats = stats_get_task(task_get_id());
    322         if (task_stats) {
    323                 total_cycles = (clock_t) (task_stats->kcycles +
    324                     task_stats->ucycles);
    325                 free(task_stats);
    326                 task_stats = 0;
    327         }
    328 
    329         return total_cycles;
    330 }
    331 
    332312int gettimeofday(struct timeval *tv, void *tz)
    333313{
Note: See TracChangeset for help on using the changeset viewer.