Motr  M0
utime.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "lib/time.h" /* m0_time_t */
24 #include "lib/time_internal.h" /* m0_clock_gettime_wrapper */
25 
26 #include "lib/assert.h" /* M0_ASSERT */
27 #include "lib/misc.h" /* M0_IN */
28 #include "lib/errno.h" /* ENOSYS */
29 
30 #include <sys/time.h> /* gettimeofday */
31 #include <time.h> /* clock_gettime */
32 
33 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_LIB
34 #include "lib/trace.h"
35 
37 {
38  struct timespec tp;
39  int rc;
40 
41  rc = clock_gettime((clockid_t)clock_id, &tp);
42  /* clock_gettime() can fail iff clock_id is invalid */
43  M0_ASSERT(rc == 0);
44  return M0_MKTIME(tp.tv_sec, tp.tv_nsec);
45 }
46 
48 {
49  struct timeval tv;
50  int rc;
51 
52  rc = gettimeofday(&tv, NULL);
53  M0_ASSERT(rc == 0);
54  return M0_MKTIME(tv.tv_sec, tv.tv_usec * 1000);
55 }
56 
59 {
60  struct timespec reqts = {
61  .tv_sec = m0_time_seconds(req),
62  .tv_nsec = m0_time_nanoseconds(req)
63  };
64  struct timespec remts;
65  int rc;
66 
67  rc = nanosleep(&reqts, &remts);
68  if (rem != NULL)
69  *rem = rc != 0 ? m0_time(remts.tv_sec, remts.tv_nsec) : 0;
70  return rc != 0 ? M0_ERR(rc) : rc;
71 }
72 M0_EXPORTED(m0_nanosleep);
73 
74 #undef M0_TRACE_SUBSYSTEM
75 
76 /*
77  * Local variables:
78  * c-indentation-style: "K&R"
79  * c-basic-offset: 8
80  * tab-width: 8
81  * fill-column: 80
82  * scroll-step: 1
83  * End:
84  */
CLOCK_SOURCES
Definition: time.h:36
#define NULL
Definition: misc.h:38
static struct io_request req
Definition: file.c:100
uint64_t m0_time_t
Definition: time.h:37
static m0_time_t tv(int index)
Definition: time.c:129
uint64_t m0_time_nanoseconds(const m0_time_t time)
Definition: time.c:89
M0_INTERNAL m0_time_t m0_clock_gettimeofday_wrapper(void)
Definition: utime.c:47
m0_time_t m0_time(uint64_t secs, long ns)
Definition: time.c:41
M0_INTERNAL m0_time_t m0_clock_gettime_wrapper(enum CLOCK_SOURCES clock_id)
Definition: utime.c:36
return M0_ERR(-EOPNOTSUPP)
#define M0_ASSERT(cond)
uint64_t m0_time_seconds(const m0_time_t time)
Definition: time.c:83
#define M0_MKTIME(secs, ns)
Definition: time.h:86
int32_t rc
Definition: trigger_fop.h:47
int m0_nanosleep(const m0_time_t req, m0_time_t *rem)
Definition: utime.c:58