blob: d0cf172aea45db4a222df609842fec31c583f0e7 [file] [log] [blame]
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002 *------------------------------------------------------------------
3 * persist.c - persistent data structure storage test / demo code
4 *
5 * Copyright (c) 2013 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <sys/types.h>
23#include <sys/mman.h>
24#include <sys/stat.h>
25#include <netinet/in.h>
26#include <signal.h>
27#include <pthread.h>
28#include <unistd.h>
29#include <time.h>
30#include <fcntl.h>
31#include <string.h>
32#include <vppinfra/clib.h>
33#include <vppinfra/vec.h>
34#include <vppinfra/hash.h>
35#include <vppinfra/bitmap.h>
36#include <vppinfra/fifo.h>
37#include <vppinfra/time.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070038#include <vppinfra/heap.h>
39#include <vppinfra/pool.h>
40#include <vppinfra/format.h>
41#include <vppinfra/serialize.h>
42#include <svmdb.h>
43
Dave Barach8a7fb0c2016-07-08 14:44:23 -040044typedef struct
45{
46 svmdb_client_t *c;
Ed Warnickecb9cada2015-12-08 15:45:58 -070047} persist_main_t;
48
49persist_main_t persist_main;
50
Dave Barach8a7fb0c2016-07-08 14:44:23 -040051typedef struct
52{
53 u8 *string1;
54 u8 *string2;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055} demo_struct2_t;
56
Dave Barach8a7fb0c2016-07-08 14:44:23 -040057typedef struct
58{
59 demo_struct2_t *demo2;
60 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070061} demo_struct1_t;
62
Dave Barach8a7fb0c2016-07-08 14:44:23 -040063/*
Ed Warnickecb9cada2015-12-08 15:45:58 -070064 * Data structures in persistent shared memory, all the time
65 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -040066clib_error_t *
67persist_malloc (persist_main_t * pm)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068{
Dave Barach8a7fb0c2016-07-08 14:44:23 -040069 demo_struct2_t *demo2;
70 demo_struct1_t *demo1;
71 time_t starttime = time (0);
72 char *datestring = ctime (&starttime);
73 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
Dave Barach8a7fb0c2016-07-08 14:44:23 -040075 /* Get back the root pointer */
76 demo1 = svmdb_local_get_variable_reference
77 (pm->c, SVMDB_NAMESPACE_VEC, "demo1_location");
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
Dave Barach8a7fb0c2016-07-08 14:44:23 -040079 /* It doesnt exist create our data structures */
80 if (demo1 == 0)
81 {
82 /* If you want MP / thread safety, lock the region... */
83 pthread_mutex_lock (&pm->c->db_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
Dave Barach8a7fb0c2016-07-08 14:44:23 -040085 /* Switch to the shared memory region heap */
86 oldheap = svm_push_data_heap (pm->c->db_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -070087
Dave Barach8a7fb0c2016-07-08 14:44:23 -040088 /* Allocate the top-level structure as a single element vector */
89 vec_validate (demo1, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
Dave Barach8a7fb0c2016-07-08 14:44:23 -040091 /* Allocate the next-level structure as a plain old memory obj */
92 demo2 = clib_mem_alloc (sizeof (*demo2));
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
Dave Barach8a7fb0c2016-07-08 14:44:23 -040094 demo1->demo2 = demo2;
95 demo1->name = format (0, "My name is Ishmael%c", 0);
96 demo2->string1 = format (0, "Here is string1%c", 0);
97 demo2->string2 = format (0, "Born at %s%c", datestring, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
Dave Barach8a7fb0c2016-07-08 14:44:23 -040099 /* Back to the process-private heap */
100 svm_pop_heap (oldheap);
101 pthread_mutex_unlock (&pm->c->db_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400103 /*
104 * Set the root pointer. Note: this guy switches heaps, locks, etc.
105 * We allocated demo1 as a vector to make this "just work..."
106 */
107 svmdb_local_set_vec_variable (pm->c, "demo1_location",
108 demo1, sizeof (demo1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
110 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400111 else
112 {
113 /* retrieve and print data from shared memory */
114 demo2 = demo1->demo2;
115 fformat (stdout, "name: %s\n", demo1->name);
116 fformat (stdout, "demo2 location: %llx\n", demo2);
117 fformat (stdout, "string1: %s\n", demo2->string1);
118 fformat (stdout, "string2: %s\n", demo2->string2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400120 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121}
122
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400123void
124unserialize_demo1 (serialize_main_t * sm, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400126 demo_struct1_t **result = va_arg (*args, demo_struct1_t **);
127 demo_struct1_t *demo1;
128 demo_struct2_t *demo2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400130 /* Allocate data structures in process private memory */
131 demo1 = clib_mem_alloc (sizeof (*demo1));
132 demo2 = clib_mem_alloc (sizeof (*demo2));
133 demo1->demo2 = demo2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400135 /* retrieve data from shared memory checkpoint */
136 unserialize_cstring (sm, (char **) &demo1->name);
137 unserialize_cstring (sm, (char **) &demo2->string1);
138 unserialize_cstring (sm, (char **) &demo2->string2);
139 *result = demo1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140}
141
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400142void
143serialize_demo1 (serialize_main_t * sm, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400145 demo_struct1_t *demo1 = va_arg (*args, demo_struct1_t *);
146 demo_struct2_t *demo2 = demo1->demo2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400148 serialize_cstring (sm, (char *) demo1->name);
149 serialize_cstring (sm, (char *) demo2->string1);
150 serialize_cstring (sm, (char *) demo2->string2);
151}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
153/* Serialize / unserialize variant */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400154clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155persist_serialize (persist_main_t * pm)
156{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400157 u8 *checkpoint;
158 serialize_main_t sm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400160 demo_struct2_t *demo2;
161 demo_struct1_t *demo1;
162 time_t starttime = time (0);
163 char *datestring = ctime (&starttime);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400165 /* Get back the root pointer */
166 checkpoint = svmdb_local_get_vec_variable (pm->c, "demo1_checkpoint",
167 sizeof (u8));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400169 /* It doesnt exist create our data structures */
170 if (checkpoint == 0)
171 {
172 /* Allocate data structures in process-private memory */
173 demo1 = clib_mem_alloc (sizeof (*demo2));
174 vec_validate (demo1, 0);
175 demo2 = clib_mem_alloc (sizeof (*demo2));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400177 demo1->demo2 = demo2;
178 demo1->name = format (0, "My name is Ishmael%c", 0);
179 demo2->string1 = format (0, "Here is string1%c", 0);
180 demo2->string2 = format (0, "Born at %s%c", datestring, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400182 /* Create checkpoint */
183 serialize_open_vector (&sm, checkpoint);
184 serialize (&sm, serialize_demo1, demo1);
185 checkpoint = serialize_close_vector (&sm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400187 /* Copy checkpoint into shared memory */
188 svmdb_local_set_vec_variable (pm->c, "demo1_checkpoint",
189 checkpoint, sizeof (u8));
190 /* Toss the process-private-memory original.. */
191 vec_free (checkpoint);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400193 else
194 {
195 /* Open the checkpoint */
196 unserialize_open_data (&sm, checkpoint, vec_len (checkpoint));
197 unserialize (&sm, unserialize_demo1, &demo1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400199 /* Toss the process-private-memory checkpoint copy */
200 vec_free (checkpoint);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400202 /* Off we go... */
203 demo2 = demo1->demo2;
204 fformat (stdout, "name: %s\n", demo1->name);
205 fformat (stdout, "demo2 location: %llx\n", demo2);
206 fformat (stdout, "string1: %s\n", demo2->string1);
207 fformat (stdout, "string2: %s\n", demo2->string2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400209 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210}
211
212
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400213int
214main (int argc, char **argv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400216 unformat_input_t _input, *input = &_input;
217 persist_main_t *pm = &persist_main;
218 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400220 /* Make a 4mb database arena, chroot so it's truly private */
221 pm->c = svmdb_map_chroot_size ("/ptest", 4 << 20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400223 ASSERT (pm->c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400225 unformat_init_command_line (input, argv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400227 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
228 {
229 if (unformat (input, "malloc"))
230 error = persist_malloc (pm);
231 else if (unformat (input, "serialize"))
232 error = persist_serialize (pm);
233 else
234 {
235 error = clib_error_return (0, "Unknown flavor '%U'",
236 format_unformat_error, input);
237 break;
238 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239 }
240
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400241 svmdb_unmap (pm->c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400243 if (error)
244 {
245 clib_error_report (error);
246 exit (1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400248 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249}
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400250
251/*
252 * fd.io coding-style-patch-verification: ON
253 *
254 * Local Variables:
255 * eval: (c-set-style "gnu")
256 * End:
257 */