summaryrefslogtreecommitdiffstats
path: root/src/lib/ppc405lib/time.c
blob: 1bc37e8bbb62154cad1b168f7b7bca4f04bfde84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/lib/ppc405lib/time.c $                                    */
/*                                                                        */
/* OpenPOWER OnChipController Project                                     */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2016                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

/// \file time.c
/// \brief Implementation of functions from <time.h>
///
/// The following standard APIs are currently supported:
///
/// - clock_gettime() with the single clock id CLOCK_REALTIME
///
/// Since SSX does not currently have any per-thread CPU time statistics, we
/// can not implement process-CPU-time-based APIs like the simple clock() or
/// clock_gettime() with CLOCK_REALTIME.

#include "ssx.h"
#include <errno.h>
#include <time.h>

/// Get time from a timer
///
/// \param clock_id This must be the constant CLOCK_REALTIME defined in
/// <time.h>. 
///
/// \param tp A pointer to a struct timespec populated by this API.
///
/// Although the Posix standard requires that clock_gettime() support
/// CLOCK_REALTIME, CLOCK_REALTIME measures standard Unix time (seconds since
/// the epoch) which is not available to SSX.  SSX currently only supports
/// CLOCK_MONOTONIC, which is derived from the SSX timebase.
///
/// \returns Either 0 for success, or -EINVAL in the event of an invalid
/// argument (unrecognized \a clock_id, NULL \a tp pointer).

int
clock_gettime(clockid_t clock_id, struct timespec* tp)
{
    int rc;
    SsxTimebase now;

    if ((clock_id != CLOCK_MONOTONIC) || (tp == 0)) {
        rc = -EINVAL;
    } else {

        now = ssx_timebase_get();
        tp->tv_sec = now / SSX_TIMEBASE_FREQUENCY_HZ;
        tp->tv_nsec = 
            ((now % SSX_TIMEBASE_FREQUENCY_HZ) * 1000000000) / 
            SSX_TIMEBASE_FREQUENCY_HZ;
        rc = 0;
    }

    return rc;
}
OpenPOWER on IntegriCloud