Motr  M0
memory.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2011-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/misc.h" /* M0_SET0 */
24 #include "lib/ub.h"
25 #include "ut/ut.h"
26 #include "lib/vec.h" /* M0_SEG_SIZE & M0_SEG_SHIFT */
27 #include "lib/memory.h"
28 
29 struct test1 {
30  int a;
31 };
32 
33 void test_memory(void)
34 {
35  void *ptr1;
36  struct test1 *ptr2;
37  size_t allocated;
38  int i;
40  ptr1 = m0_alloc(100);
41  M0_UT_ASSERT(ptr1 != NULL);
42 
43  M0_ALLOC_PTR(ptr2);
44  M0_UT_ASSERT(ptr2 != NULL);
45 
46  m0_free(ptr1);
47  /*
48  * +16, because free(3) may use first 16 bytes for its nefarious
49  * purposes.
50  */
51 #if defined(ENABLE_FREE_POISON)
52  M0_UT_ASSERT(m0_is_poisoned((char *)ptr1 + 16));
53 #endif
54  m0_free(ptr2);
56 
57  /* Checking m0_alloc_aligned for buffer sizes from 4K to 64Kb. */
58  for (i = 0; i <= M0_SEG_SIZE * 16; i += M0_SEG_SIZE / 2) {
59  ptr1 = m0_alloc_aligned(i, M0_SEG_SHIFT);
60  M0_UT_ASSERT(m0_addr_is_aligned(ptr1, M0_SEG_SHIFT));
61  m0_free_aligned(ptr1, (size_t)i, M0_SEG_SHIFT);
62  }
63 
64 }
65 
66 enum {
67  UB_ITER = 5000000,
68  UB_SMALL = 1,
69  UB_MEDIUM = 17,
70  UB_LARGE = 512,
71  UB_HUGE = 128*1024
72 };
73 
74 static void *ubx[UB_ITER];
75 
76 static int ub_init(const char *opts M0_UNUSED)
77 {
79  return 0;
80 }
81 
82 static void ub_free(int i)
83 {
84  m0_free(ubx[i]);
85 }
86 
87 static void ub_small(int i)
88 {
89  ubx[i] = m0_alloc(UB_SMALL);
90 }
91 
92 static void ub_medium(int i)
93 {
94  ubx[i] = m0_alloc(UB_MEDIUM);
95 }
96 
97 static void ub_large(int i)
98 {
99  ubx[i] = m0_alloc(UB_LARGE);
100 }
101 
102 static void ub_huge(int i)
103 {
104  ubx[i] = m0_alloc(UB_HUGE);
105 }
106 
107 #if 0
108 static void ub_free_all(void)
109 {
110  int i;
111 
112  for (i = 0; i < ARRAY_SIZE(ubx); ++i)
113  m0_free(ubx[i]);
114 }
115 #endif
116 
118  .us_name = "memory-ub",
119  .us_init = ub_init,
120  .us_fini = NULL,
121  .us_run = {
122  { .ub_name = "alloc-small",
123  .ub_iter = UB_ITER,
124  .ub_round = ub_small },
125 
126  { .ub_name = "free-small",
127  .ub_iter = UB_ITER,
128  .ub_round = ub_free },
129 
130  { .ub_name = "alloc-medium",
131  .ub_iter = UB_ITER,
132  .ub_round = ub_medium },
133 
134  { .ub_name = "free-medium",
135  .ub_iter = UB_ITER,
136  .ub_round = ub_free },
137 
138  { .ub_name = "alloc-large",
139  .ub_iter = UB_ITER,
140  .ub_round = ub_large },
141 
142  { .ub_name = "free-large",
143  .ub_iter = UB_ITER,
144  .ub_round = ub_free },
145 
146  { .ub_name = "alloc-huge",
147  .ub_iter = UB_ITER/1000,
148  .ub_round = ub_huge },
149 
150  { .ub_name = "free-huge",
151  .ub_iter = UB_ITER/1000,
152  .ub_round = ub_free },
153 
154  { .ub_name = NULL }
155  }
156 };
157 
158 
159 /*
160  * Local variables:
161  * c-indentation-style: "K&R"
162  * c-basic-offset: 8
163  * tab-width: 8
164  * fill-column: 80
165  * scroll-step: 1
166  * End:
167  */
static void ub_huge(int i)
Definition: memory.c:102
#define NULL
Definition: misc.h:38
static void ub_small(int i)
Definition: memory.c:87
static bool m0_addr_is_aligned(const void *addr, unsigned shift)
Definition: memory.h:107
static void ub_medium(int i)
Definition: memory.c:92
Definition: memory.c:71
Definition: list.c:27
M0_INTERNAL void m0_free_aligned(void *data, size_t size, unsigned shift)
Definition: memory.c:192
Definition: memory.c:67
static struct m0_atomic64 allocated
Definition: memory.c:101
M0_INTERNAL size_t m0_allocated(void)
Definition: memory.c:215
static void ub_large(int i)
Definition: memory.c:97
int i
Definition: dir.c:1033
#define M0_SET_ARR0(arr)
Definition: misc.h:72
static int ub_init(const char *opts M0_UNUSED)
Definition: memory.c:76
struct m0_ub_set m0_memory_ub
Definition: memory.c:117
const char * us_name
Definition: ub.h:76
void * m0_alloc(size_t size)
Definition: memory.c:126
M0_INTERNAL bool m0_is_poisoned(const void *ptr)
Definition: memory.c:79
static void ub_free(int i)
Definition: memory.c:82
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
int a
Definition: memory.c:30
M0_INTERNAL void * m0_alloc_aligned(size_t size, unsigned shift)
Definition: memory.c:168
void test_memory(void)
Definition: memory.c:33
void m0_free(void *data)
Definition: memory.c:146
static void * ubx[UB_ITER]
Definition: memory.c:74
#define ARRAY_SIZE(a)
Definition: misc.h:45
Definition: ub.h:74
#define M0_UT_ASSERT(a)
Definition: ut.h:46
#define M0_UNUSED
Definition: misc.h:380