Changeset bd41ac52 in mainline for uspace/app


Ignore:
Timestamp:
2018-08-25T22:21:25Z (8 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
cca80a2
Parents:
e2625b1a
Message:

Get rid of sys/time.h

This commit moves the POSIX-like time functionality from libc's
sys/time.h to libposix and introduces C11-like or HelenOS-specific
interfaces to libc.

Specifically, use of sys/time.h, struct timeval, suseconds_t and
gettimeofday is replaced by time.h (C11), struct timespec (C11), usec_t
(HelenOS) and getuptime / getrealtime (HelenOS).

Also attempt to fix the implementation of clock() to return microseconds
(clocks) rather than processor cycles and move it to libc.

Location:
uspace/app
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/barber/barber.c

    re2625b1a rbd41ac52  
    130130}
    131131
    132 static void plan_frame_timer(suseconds_t render_time)
     132static void plan_frame_timer(usec_t render_time)
    133133{
    134134        /*
     
    139139         */
    140140
    141         suseconds_t delta = 1000000 / fps;
     141        usec_t delta = 1000000 / fps;
    142142        load_t load = get_load();
    143143
     
    190190static void frame_timer_callback(void *data)
    191191{
    192         struct timeval prev;
     192        struct timespec prev;
    193193        getuptime(&prev);
    194194
     
    199199        update_canvas(frame_canvas, frames[frame]);
    200200
    201         struct timeval cur;
     201        struct timespec cur;
    202202        getuptime(&cur);
    203203
    204         plan_frame_timer(tv_sub_diff(&cur, &prev));
     204        plan_frame_timer(NSEC2USEC(ts_sub_diff(&cur, &prev)));
    205205}
    206206
  • uspace/app/bdsh/cmds/modules/sleep/sleep.c

    re2625b1a rbd41ac52  
    5757}
    5858
    59 /** Convert string containing decimal seconds to useconds_t.
     59/** Convert string containing decimal seconds to usec_t.
    6060 *
    6161 * @param nptr   Pointer to string.
     
    6363 * @return EOK if conversion was successful.
    6464 */
    65 static errno_t decimal_to_useconds(const char *nptr, useconds_t *result)
     65static errno_t decimal_to_useconds(const char *nptr, usec_t *result)
    6666{
    6767        errno_t ret;
    68         uint64_t whole_seconds;
    69         uint64_t frac_seconds;
     68        sec_t whole_seconds;
     69        usec_t frac_seconds;
    7070        const char *endptr;
    7171
     
    7575                endptr = (char *)nptr;
    7676        } else {
    77                 ret = str_uint64_t(nptr, &endptr, 10, false, &whole_seconds);
     77                ret = str_int64_t(nptr, &endptr, 10, false, &whole_seconds);
    7878                if (ret != EOK)
    7979                        return ret;
     
    8787        } else if (*endptr == '.') {
    8888                nptr = endptr + 1;
    89                 ret = str_uint64_t(nptr, &endptr, 10, true, &frac_seconds);
     89                ret = str_int64_t(nptr, &endptr, 10, true, &frac_seconds);
    9090                if (ret != EOK)
    9191                        return ret;
     
    101101
    102102        /* Check for overflow */
    103         useconds_t total = whole_seconds * 1000000 + frac_seconds;
    104         if (total / 1000000 != whole_seconds)
     103        usec_t total = SEC2USEC(whole_seconds) + frac_seconds;
     104        if (USEC2SEC(total) != whole_seconds)
    105105                return EOVERFLOW;
    106106
     
    115115        errno_t ret;
    116116        unsigned int argc;
    117         useconds_t duration = 0;
     117        usec_t duration = 0;
    118118
    119119        /* Count the arguments */
  • uspace/app/bnchmark/bnchmark.c

    re2625b1a rbd41ac52  
    5656
    5757typedef errno_t (*measure_func_t)(void *);
    58 typedef unsigned long umseconds_t; /* milliseconds */
    5958
    6059static void syntax_print(void);
    6160
    62 static errno_t measure(measure_func_t fn, void *data, umseconds_t *result)
    63 {
    64         struct timeval start_time;
    65         gettimeofday(&start_time, NULL);
     61static errno_t measure(measure_func_t fn, void *data, msec_t *result)
     62{
     63        struct timespec start_time;
     64        getuptime(&start_time);
    6665
    6766        errno_t rc = fn(data);
     
    7170        }
    7271
    73         struct timeval final_time;
    74         gettimeofday(&final_time, NULL);
     72        struct timespec final_time;
     73        getuptime(&final_time);
    7574
    7675        /* Calculate time difference in milliseconds */
    77         *result = ((final_time.tv_usec - start_time.tv_usec) / 1000) +
    78             ((final_time.tv_sec - start_time.tv_sec) * 1000);
     76        *result = NSEC2USEC(ts_sub_diff(&final_time, &start_time));
    7977        return EOK;
    8078}
     
    133131{
    134132        errno_t rc;
    135         umseconds_t milliseconds_taken = 0;
     133        msec_t milliseconds_taken = 0;
    136134        char *path = NULL;
    137135        measure_func_t fn = NULL;
     
    194192                }
    195193
    196                 printf("%s;%s;%s;%lu;ms\n", test_type, path, log_str, milliseconds_taken);
     194                printf("%s;%s;%s;%lld;ms\n", test_type, path, log_str, milliseconds_taken);
    197195        }
    198196
  • uspace/app/modplay/modplay.c

    re2625b1a rbd41ac52  
    7575        console_ctrl_t *con;
    7676        cons_event_t event;
    77         suseconds_t timeout;
     77        usec_t timeout;
    7878        pcm_format_t format;
    7979        void *buffer;
  • uspace/app/stats/stats.c

    re2625b1a rbd41ac52  
    190190static void print_uptime(void)
    191191{
    192         struct timeval uptime;
     192        struct timespec uptime;
    193193        getuptime(&uptime);
    194194
    195         printf("%s: Up %ld days, %ld hours, %ld minutes, %ld seconds\n",
     195        printf("%s: Up %lld days, %lld hours, %lld minutes, %lld seconds\n",
    196196            NAME, uptime.tv_sec / DAY, (uptime.tv_sec % DAY) / HOUR,
    197197            (uptime.tv_sec % HOUR) / MINUTE, uptime.tv_sec % MINUTE);
  • uspace/app/tester/ipc/ping_pong.c

    re2625b1a rbd41ac52  
    2929#include <stdio.h>
    3030#include <stdlib.h>
    31 #include <sys/time.h>
     31#include <time.h>
    3232#include <ns.h>
    3333#include <async.h>
     
    4242        TPRINTF("Pinging ns server for %d seconds...", DURATION_SECS);
    4343
    44         struct timeval start;
    45         gettimeofday(&start, NULL);
     44        struct timespec start;
     45        getuptime(&start);
    4646
    4747        uint64_t count = 0;
    4848        while (true) {
    49                 struct timeval now;
    50                 gettimeofday(&now, NULL);
     49                struct timespec now;
     50                getuptime(&now);
    5151
    52                 if (tv_sub_diff(&now, &start) >= DURATION_SECS * 1000000L)
     52                if (NSEC2SEC(ts_sub_diff(&now, &start)) >= DURATION_SECS)
    5353                        break;
    5454
  • uspace/app/tester/ipc/starve.c

    re2625b1a rbd41ac52  
    2929#include <stdio.h>
    3030#include <stdlib.h>
    31 #include <sys/time.h>
     31#include <time.h>
    3232#include <io/console.h>
    3333#include <async.h>
     
    4343                return "Failed to init connection with console.";
    4444
    45         struct timeval start;
    46         gettimeofday(&start, NULL);
     45        struct timespec start;
     46        getuptime(&start);
    4747
    4848        TPRINTF("Intensive computation shall be imagined (for %ds)...\n", DURATION_SECS);
    4949        TPRINTF("Press a key to terminate prematurely...\n");
    5050        while (true) {
    51                 struct timeval now;
    52                 gettimeofday(&now, NULL);
     51                struct timespec now;
     52                getuptime(&now);
    5353
    54                 if (tv_sub_diff(&now, &start) >= DURATION_SECS * 1000000L)
     54                if (NSEC2SEC(ts_sub_diff(&now, &start)) >= DURATION_SECS)
    5555                        break;
    5656
    5757                cons_event_t ev;
    58                 suseconds_t timeout = 0;
     58                usec_t timeout = 0;
    5959                bool has_event = console_get_event_timeout(console, &ev, &timeout);
    6060                if (has_event && ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS) {
  • uspace/app/testread/testread.c

    re2625b1a rbd41ac52  
    126126        next_mark = 0;
    127127        last_mark = 0;
    128         struct timeval prev_time;
    129         struct timeval start_time;
    130         gettimeofday(&start_time, NULL);
     128        struct timespec prev_time;
     129        struct timespec start_time;
     130        getuptime(&start_time);
    131131        prev_time = start_time;
    132132
     
    152152
    153153                if (progress && offset >= next_mark) {
    154                         struct timeval cur_time;
    155                         gettimeofday(&cur_time, NULL);
     154                        struct timespec cur_time;
     155                        getuptime(&cur_time);
    156156
    157157                        uint32_t last_run = cur_time.tv_sec - prev_time.tv_sec;
     
    170170        }
    171171
    172         struct timeval final_time;
    173         gettimeofday(&final_time, NULL);
     172        struct timespec final_time;
     173        getuptime(&final_time);
    174174
    175175        uint32_t total_run_time = final_time.tv_sec - start_time.tv_sec;
  • uspace/app/tetris/scores.h

    re2625b1a rbd41ac52  
    5454 */
    5555
    56 #include <sys/time.h>
     56#include <time.h>
    5757#include <str.h>
    5858
  • uspace/app/tetris/screen.c

    re2625b1a rbd41ac52  
    7575static const struct shape *lastshape;
    7676
    77 static suseconds_t timeleft = 0;
     77static usec_t timeleft = 0;
    7878
    7979console_ctrl_t *console;
     
    340340void tsleep(void)
    341341{
    342         suseconds_t timeout = fallrate;
     342        usec_t timeout = fallrate;
    343343
    344344        while (timeout > 0) {
  • uspace/app/tetris/tetris.c

    re2625b1a rbd41ac52  
    5555    "\tThe Regents of the University of California.  All rights reserved.\n";
    5656
    57 #include <sys/time.h>
     57#include <time.h>
    5858#include <errno.h>
    5959#include <stdbool.h>
     
    170170static void srandomdev(void)
    171171{
    172         struct timeval tv;
    173 
    174         gettimeofday(&tv, NULL);
    175         srand(tv.tv_sec + tv.tv_usec / 100000);
     172        struct timespec ts;
     173
     174        getrealtime(&ts);
     175        srand(ts.tv_sec + ts.tv_nsec / 100000000);
    176176}
    177177
  • uspace/app/top/screen.c

    re2625b1a rbd41ac52  
    4949#include "top.h"
    5050
    51 #define USEC_COUNT  1000000
    52 
    53 static suseconds_t timeleft = 0;
     51static usec_t timeleft = 0;
    5452
    5553console_ctrl_t *console;
     
    5755static sysarg_t warning_col = 0;
    5856static sysarg_t warning_row = 0;
    59 static suseconds_t warning_timeleft = 0;
     57static usec_t warning_timeleft = 0;
    6058static char *warning_text = NULL;
    6159
     
    179177static inline void print_global_head(data_t *data)
    180178{
    181         printf("top - %02lu:%02lu:%02lu up "
     179        printf("top - %02lld:%02lld:%02lld up "
    182180            "%" PRIun " days, %02" PRIun ":%02" PRIun ":%02" PRIun ", "
    183181            "load average:",
     
    527525        va_end(args);
    528526
    529         warning_timeleft = 2 * USEC_COUNT;
     527        warning_timeleft = SEC2USEC(2);
    530528
    531529        screen_moveto(warning_col, warning_row);
     
    537535 *
    538536 */
    539 int tgetchar(unsigned int sec)
     537int tgetchar(sec_t sec)
    540538{
    541539        /*
     
    544542
    545543        if (timeleft <= 0)
    546                 timeleft = sec * USEC_COUNT;
     544                timeleft = SEC2USEC(sec);
    547545
    548546        /*
  • uspace/app/top/screen.h

    re2625b1a rbd41ac52  
    4747    _HELENOS_PRINTF_ATTRIBUTE(1, 2);
    4848
    49 extern int tgetchar(unsigned int);
     49extern int tgetchar(sec_t);
    5050
    5151#endif
  • uspace/app/top/top.c

    re2625b1a rbd41ac52  
    3939#include <stdlib.h>
    4040#include <task.h>
    41 #include <sys/time.h>
     41#include <time.h>
    4242#include <errno.h>
    4343#include <gsort.h>
     
    154154
    155155        /* Get current time */
    156         struct timeval time;
    157         gettimeofday(&time, NULL);
     156        struct timespec time;
     157        getrealtime(&time);
    158158
    159159        target->hours = (time.tv_sec % DAY) / HOUR;
     
    162162
    163163        /* Get uptime */
    164         struct timeval uptime;
     164        struct timespec uptime;
    165165        getuptime(&uptime);
    166166
  • uspace/app/wavplay/dplay.c

    re2625b1a rbd41ac52  
    4242#include <pcm/format.h>
    4343#include <as.h>
    44 #include <sys/time.h>
     44#include <time.h>
    4545#include <inttypes.h>
    4646#include <stdbool.h>
     
    242242
    243243#define DPRINTF(f, ...) \
    244         printf("%.2lu:%.6lu   "f, time.tv_sec % 100, time.tv_usec, __VA_ARGS__)
     244        printf("%.2lld:%.6lld   "f, time.tv_sec % 100, \
     245            NSEC2USEC(time.tv_nsec), __VA_ARGS__)
    245246
    246247/**
     
    255256        printf("Playing: %dHz, %s, %d channel(s).\n", pb->f.sampling_rate,
    256257            pcm_sample_format_str(pb->f.sample_format), pb->f.channels);
    257         useconds_t work_time = 50000; /* 50 ms */
     258        usec_t work_time = 50000; /* 50 ms */
    258259        bool started = false;
    259260        size_t pos = 0;
    260         struct timeval time = { 0 };
     261        struct timespec time = { 0 };
    261262        getuptime(&time);
    262263        while (true) {
     
    303304                }
    304305                const size_t to_play = buffer_occupied(pb, pos);
    305                 const useconds_t usecs =
    306                     pcm_format_size_to_usec(to_play, &pb->f);
     306                const usec_t usecs = pcm_format_size_to_usec(to_play, &pb->f);
    307307
    308308                /* Compute delay time */
    309                 const useconds_t real_delay = (usecs > work_time) ?
     309                const usec_t real_delay = (usecs > work_time) ?
    310310                    usecs - work_time : 0;
    311                 DPRINTF("POS %zu: %u usecs (%u) to play %zu bytes.\n",
     311                DPRINTF("POS %zu: %lld usecs (%lld) to play %zu bytes.\n",
    312312                    pos, usecs, real_delay, to_play);
    313313                if (real_delay)
Note: See TracChangeset for help on using the changeset viewer.