Motr  M0
ktime.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 #include "lib/misc.h" /* M0_EXPORTED */
26 
27 #include <linux/module.h>
28 #include <linux/time.h>
29 #include <linux/jiffies.h>
30 #include <linux/sched.h>
31 
41 {
42  struct timespec ts;
43  m0_time_t ret;
44 
45  switch (clock_id) {
47  getrawmonotonic(&ts);
48  ret = M0_MKTIME(ts.tv_sec, ts.tv_nsec);
49  break;
51  /* ts = current_kernel_time(); */
52  getnstimeofday(&ts);
53  ret = M0_MKTIME(ts.tv_sec, ts.tv_nsec);
54  break;
55  default:
56  M0_IMPOSSIBLE("Unknown clock source");
57  ret = M0_MKTIME(0, 0);
58  }
59  return ret;
60 }
61 
63 {
64  struct timespec ts;
65 
66  getnstimeofday(&ts);
67  return M0_MKTIME(ts.tv_sec, ts.tv_nsec);
68 }
69 
74 {
75  struct timespec ts = {
76  .tv_sec = m0_time_seconds(req),
77  .tv_nsec = m0_time_nanoseconds(req)
78  };
79  unsigned long tj = timespec_to_jiffies(&ts);
80  unsigned long remtj;
81  struct timespec remts;
82 
83  /* this may use schedule_timeout_interruptible() to capture signals */
84  remtj = schedule_timeout_uninterruptible(tj);
85  if (rem != NULL) {
86  jiffies_to_timespec(remtj, &remts);
87  *rem = m0_time(remts.tv_sec, remts.tv_nsec);
88  }
89  return remtj == 0 ? 0 : -1;
90 }
91 M0_EXPORTED(m0_nanosleep);
92 
95 /*
96  * Local variables:
97  * c-indentation-style: "K&R"
98  * c-basic-offset: 8
99  * tab-width: 8
100  * fill-column: 80
101  * scroll-step: 1
102  * End:
103  */
M0_INTERNAL m0_time_t m0_clock_gettimeofday_wrapper(void)
Definition: ktime.c:62
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
uint64_t m0_time_nanoseconds(const m0_time_t time)
Definition: time.c:89
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: ktime.c:40
uint64_t m0_time_seconds(const m0_time_t time)
Definition: time.c:83
#define M0_MKTIME(secs, ns)
Definition: time.h:86
#define M0_IMPOSSIBLE(fmt,...)
int m0_nanosleep(const m0_time_t req, m0_time_t *rem)
Definition: ktime.c:73