Motr  M0
st_misc.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 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 
26 #ifdef __KERNEL__
27 
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/time.h>
32 #include <linux/slab.h>
33 #include <linux/sched.h>
34 #include <linux/random.h>
35 
36 #else
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <stdarg.h>
40 #include <sys/time.h>
41 #include <sys/types.h>
42 #include <unistd.h> /* syscall (gettid)*/
43 #include <sys/syscall.h> /* syscall (gettid) */
44 
45 #endif
46 
47 #include <stdbool.h>
48 
49 #include "motr/st/st_assert.h"
50 #include "motr/st/st_misc.h"
51 #include "lib/memory.h"
52 
53 /*******************************************************************
54  * Time *
55  *******************************************************************/
56 
57 #ifdef __KERNEL__
58 
59 uint64_t time_now(void)
60 {
61  struct timespec ts;
62 
63  getnstimeofday(&ts);
64  return ts.tv_sec * TIME_ONE_SECOND + ts.tv_nsec;
65 }
66 
67 #else
68 
69 uint64_t time_now(void)
70 {
71  struct timeval tv;
72 
73  gettimeofday(&tv, NULL);
74  return tv.tv_sec * TIME_ONE_SECOND + tv.tv_usec * 1000;
75 }
76 
77 #endif /* end of time_now*/
78 
79 uint64_t time_seconds(const uint64_t time)
80 {
81  return time / TIME_ONE_SECOND;
82 }
83 
84 uint64_t time_nanoseconds(const uint64_t time)
85 {
86  return time % TIME_ONE_SECOND;
87 }
88 
89 uint64_t time_from_now(uint64_t secs, uint64_t ns)
90 {
91  return time_now() + secs * TIME_ONE_SECOND + ns;
92 }
93 
94 
95 /*******************************************************************
96  * Memory *
97  *******************************************************************/
98 
99 void *mem_alloc(size_t size)
100 {
101  void *p;
102 
103  p = m0_alloc(size);
104  if (p != NULL)
105  memset(p, 0, size);
106 
107  if (st_is_cleaner_up() == true)
108  st_mark_ptr(p);
109 
110  return p;
111 }
112 
113 void mem_free(void *p)
114 {
115  if (st_is_cleaner_up() == true)
116  st_unmark_ptr(p);
117 
118  m0_free(p);
119 }
120 
121 /*******************************************************************
122  * Misc *
123  *******************************************************************/
124 
125 #ifdef __KERNEL__
126 pid_t get_tid(void)
127 {
128  return current->pid;
129 }
130 
131 void console_printf(const char *fmt, ...)
132 {
133  va_list args;
134 
135  va_start(args, fmt);
136  vprintk(fmt, args);
137  va_end(args);
138 }
139 
140 uint32_t generate_random(uint32_t max)
141 {
142  uint32_t randv;
143 
144  get_random_bytes(&randv, sizeof(randv));
145  return randv % max;
146 }
147 
148 #else
149 
150 pid_t get_tid(void)
151 {
152  return syscall(SYS_gettid);
153 }
154 
155 void console_printf(const char *fmt, ...)
156 {
157  va_list args;
158 
159  va_start(args, fmt);
160  vprintf(fmt, args);
161  va_end(args);
162 }
163 
164 uint32_t generate_random(uint32_t max)
165 {
166  return (uint32_t)random()%max;
167 }
168 
169 #endif
170 
171 /*
172  * Local variables:
173  * c-indentation-style: "K&R"
174  * c-basic-offset: 8
175  * tab-width: 8
176  * fill-column: 80
177  * scroll-step: 1
178  * End:
179  */
static struct m0_addb2_philter p
Definition: consumer.c:40
#define NULL
Definition: misc.h:38
void mem_free(void *p)
Definition: st_misc.c:113
static m0_time_t tv(int index)
Definition: time.c:129
Definition: ub.c:49
def args
Definition: addb2db.py:716
static long long max(long long a, long long b)
Definition: crate.c:196
char * fmt(const char *format,...) __attribute__((format(printf
void * m0_alloc(size_t size)
Definition: memory.c:126
void st_mark_ptr(void *ptr)
Definition: st_assert.c:218
void console_printf(const char *fmt,...)
Definition: st_misc.c:155
uint64_t time_now(void)
Definition: st_misc.c:69
void st_unmark_ptr(void *ptr)
Definition: st_assert.c:239
m0_bcount_t size
Definition: di.c:39
bool st_is_cleaner_up()
Definition: st_assert.c:326
uint32_t generate_random(uint32_t max)
Definition: st_misc.c:164
uint64_t time_seconds(const uint64_t time)
Definition: st_misc.c:79
pid_t get_tid(void)
Definition: st_misc.c:150
void m0_free(void *data)
Definition: memory.c:146
void * mem_alloc(size_t size)
Definition: st_misc.c:99
uint64_t time_nanoseconds(const uint64_t time)
Definition: st_misc.c:84
uint64_t time_from_now(uint64_t secs, uint64_t ns)
Definition: st_misc.c:89