Motr  M0
client.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2020 Seagate Technology LLC and/or its Affiliates
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * For any questions about this software or licensing,
17  * please email opensource@seagate.com or cortx-questions@seagate.com.
18  *
19  */
20 
21 
22 #include <stdio.h>
23 
24 #include "lib/assert.h"
25 #include "motr/magic.h"
26 #include "desim/sim.h"
27 #include "desim/net.h"
28 #include "desim/client.h"
29 
37  unsigned ctp_client;
38  unsigned ctp_thread;
39 };
40 
42  unsigned long long cwe_offset;
43  unsigned long cwe_count;
45  uint64_t cwe_magic;
46 };
47 
48 M0_TL_DESCR_DEFINE(cl, "client write extents", static, struct client_write_ext,
49  cwe_linkage, cwe_magic, M0_DESIM_CLIENT_WRITE_EXT_MAGIC,
51 M0_TL_DEFINE(cl, static, struct client_write_ext);
52 
53 static void client_pageout(struct sim *s, struct sim_thread *t, void *arg)
54 {
55  struct client *c = arg;
56  struct client_conf *conf = c->cl_conf;
57  unsigned size = conf->cc_opt_count;
58  struct client_write_ext *ext;
59  struct m0_stob_id stob_id;
60 
61  while (1) {
62  while (c->cl_dirty - c->cl_io < size) {
63  sim_chan_wait(&c->cl_cache_busy, t);
64  if (conf->cc_shutdown)
66  }
67  M0_ASSERT(!cl_tlist_is_empty(&c->cl_write_ext));
68  ext = cl_tlist_head(&c->cl_write_ext);
69  /* no real cache management for now */
70  M0_ASSERT(ext->cwe_count == size);
71  cl_tlink_del_fini(ext);
72  sim_log(s, SLL_TRACE, "P%2i/%2i: %6lu %10llu %8u\n", c->cl_id,
73  c->cl_inflight, c->cl_fid, ext->cwe_offset, size);
74  c->cl_io += size;
75  c->cl_inflight++;
76  m0_stob_id_make(0, c->cl_fid, &M0_FID_INIT(0, c->cl_fid), &stob_id);
77  net_rpc_process(t, conf->cc_net, conf->cc_srv,
78  &stob_id, ext->cwe_offset, size);
79  c->cl_inflight--;
80  c->cl_io -= size;
81  M0_ASSERT(c->cl_cached >= size);
82  M0_ASSERT(c->cl_dirty >= size);
83  c->cl_cached -= size;
84  c->cl_dirty -= size;
85  sim_chan_broadcast(&c->cl_cache_free);
86  sim_free(ext);
87  }
88 }
89 
90 static void client_write_loop(struct sim *s, struct sim_thread *t, void *arg)
91 {
92  struct client_thread_param *param = arg;
93  unsigned clid = param->ctp_client;
94  unsigned trid = param->ctp_thread;
95  struct client_conf *conf = param->ctp_conf;
96  struct client *cl = &conf->cc_client[clid];
97  unsigned long nob = 0;
98  unsigned count = conf->cc_count;
99  unsigned long long off = conf->cc_total * trid;
100  struct client_write_ext *ext;
101 
102  M0_ASSERT(t == &cl->cl_thread[trid]);
103  M0_ASSERT(cl->cl_id == clid);
104 
105  while (nob < conf->cc_total) {
106  sim_sleep(t, sim_rnd(conf->cc_delay_min, conf->cc_delay_max));
107  while (cl->cl_dirty + count > conf->cc_dirty_max)
109  ext = sim_alloc(sizeof *ext);
110  ext->cwe_offset = off;
111  ext->cwe_count = count;
112  cl_tlink_init_at_tail(ext, &cl->cl_write_ext);
113  cl->cl_cached += count;
114  cl->cl_dirty += count;
116  sim_log(s, SLL_TRACE, "W%2i/%2i: %6lu %10llu %8u\n",
117  clid, trid, cl->cl_fid, off, count);
118  nob += count;
119  off += count;
120  }
122 }
123 
124 static int client_threads_start(struct sim_callout *call)
125 {
126  struct client_conf *conf = call->sc_datum;
127  struct client_thread_param param = {
128  .ctp_conf = conf
129  };
130  unsigned i;
131  unsigned j;
132 
133  for (i = 0; i < conf->cc_nr_clients; ++i) {
134  struct client *c = &conf->cc_client[i];
135  for (j = 0; j < conf->cc_nr_threads; ++j) {
136  param.ctp_client = i;
137  param.ctp_thread = j;
138  sim_thread_init(call->sc_sim, &c->cl_thread[j], 0,
140  }
141  for (j = 0; j < conf->cc_inflight_max; ++j)
142  sim_thread_init(call->sc_sim, &c->cl_pageout[j], 0,
143  client_pageout, c);
144  }
145  return 1;
146 }
147 
148 M0_INTERNAL void client_init(struct sim *s, struct client_conf *conf)
149 {
150  unsigned i;
151 
152  cnt_init(&conf->cc_cache_free, NULL, "client::cache-free");
153  cnt_init(&conf->cc_cache_busy, NULL, "client::cache-busy");
154  conf->cc_client = sim_alloc(conf->cc_nr_clients *
155  sizeof conf->cc_client[0]);
156  for (i = 0; i < conf->cc_nr_clients; ++i) {
157  struct client *c;
158 
159  c = &conf->cc_client[i];
160  cl_tlist_init(&c->cl_write_ext);
161  c->cl_conf = conf;
162  c->cl_fid = i;
163  c->cl_id = i;
164  sim_chan_init(&c->cl_cache_free, "client-%04i::cache-free", i);
165  sim_chan_init(&c->cl_cache_busy, "client-%04i::cache-busy", i);
166  c->cl_cache_free.ch_cnt_sleep.c_parent = &conf->cc_cache_free;
167  c->cl_cache_busy.ch_cnt_sleep.c_parent = &conf->cc_cache_busy;
168 
169  c->cl_thread = sim_alloc(conf->cc_nr_threads *
170  sizeof c->cl_thread[0]);
171  c->cl_pageout = sim_alloc(conf->cc_inflight_max *
172  sizeof c->cl_pageout[0]);
173  }
175 }
176 
177 M0_INTERNAL void client_fini(struct client_conf *conf)
178 {
179  unsigned i;
180  unsigned j;
181 
182  conf->cc_shutdown = 1;
183  if (conf->cc_client != NULL) {
184  for (i = 0; i < conf->cc_nr_clients; ++i) {
185  struct client *c = &conf->cc_client[i];
186 
187  sim_chan_broadcast(&c->cl_cache_busy);
188  sim_chan_fini(&c->cl_cache_free);
189  sim_chan_fini(&c->cl_cache_busy);
190  if (c->cl_thread != NULL) {
191  for (j = 0; j < conf->cc_nr_threads; ++j) {
192  /*
193  * run simulation to drain events posted
194  * during finalisation (e.g., by
195  * sim_chan_broadcast() above).
196  */
197  sim_run(c->cl_thread[j].st_sim);
198  sim_thread_fini(&c->cl_thread[j]);
199  }
200  sim_free(c->cl_thread);
201  }
202  if (c->cl_pageout != NULL) {
203  for (j = 0; j < conf->cc_inflight_max; ++j) {
204  sim_run(c->cl_pageout[j].st_sim);
205  sim_thread_fini(&c->cl_pageout[j]);
206  }
207  sim_free(c->cl_pageout);
208  }
209  cl_tlist_fini(&c->cl_write_ext);
210  }
211  sim_free(conf->cc_client);
212  }
213  cnt_fini(&conf->cc_cache_free);
214  cnt_fini(&conf->cc_cache_busy);
215 }
216 
219 /*
220  * Local variables:
221  * c-indentation-style: "K&R"
222  * c-basic-offset: 8
223  * tab-width: 8
224  * fill-column: 80
225  * scroll-step: 1
226  * End:
227  */
M0_INTERNAL void sim_thread_init(struct sim *state, struct sim_thread *thread, unsigned stacksize, sim_func_t func, void *arg)
Definition: sim.c:293
Definition: sim.h:152
static struct m0_config conf
Definition: client.c:48
unsigned long cl_fid
Definition: client.h:43
#define NULL
Definition: misc.h:38
M0_INTERNAL void sim_chan_fini(struct sim_chan *chan)
Definition: sim.c:400
M0_INTERNAL void sim_free(void *ptr)
Definition: sim.c:98
#define M0_FID_INIT(container, key)
Definition: fid.h:84
uint64_t cwe_magic
Definition: client.c:45
M0_INTERNAL void sim_log(struct sim *s, enum sim_log_level level, const char *format,...)
Definition: sim.c:527
Definition: conf.py:1
M0_INTERNAL void client_init(struct sim *s, struct client_conf *conf)
Definition: client.c:148
static void client_pageout(struct sim *s, struct sim_thread *t, void *arg)
Definition: client.c:53
M0_INTERNAL void sim_chan_broadcast(struct sim_chan *chan)
Definition: sim.c:456
unsigned ctp_client
Definition: client.c:37
struct m0_tl cl_write_ext
Definition: client.h:49
M0_INTERNAL void cnt_init(struct cnt *cnt, struct cnt *parent, const char *format,...)
Definition: cnt.c:44
M0_INTERNAL void net_rpc_process(struct sim_thread *t, struct net_conf *net, struct net_srv *srv, struct m0_stob_id *stob_id, unsigned long long offset, unsigned long count)
Definition: net.c:222
static m0_bcount_t count
Definition: xcode.c:167
M0_INTERNAL void sim_chan_init(struct sim_chan *chan, char *format,...)
Definition: sim.c:385
M0_INTERNAL void sim_thread_exit(struct sim_thread *thread)
Definition: sim.c:346
struct m0_tlink cwe_linkage
Definition: client.c:44
M0_INTERNAL void sim_run(struct sim *state)
Definition: sim.c:129
int i
Definition: dir.c:1033
M0_TL_DEFINE(cl, static, struct client_write_ext)
M0_INTERNAL void sim_thread_fini(struct sim_thread *thread)
Definition: sim.c:333
struct sim_thread * cl_thread
Definition: client.h:38
struct sim_chan cl_cache_free
Definition: client.h:46
static void client_write_loop(struct sim *s, struct sim_thread *t, void *arg)
Definition: client.c:90
unsigned long cl_dirty
Definition: client.h:41
#define M0_ASSERT(cond)
static struct m0_addb2_callback c
Definition: consumer.c:41
static struct m0_thread t[8]
Definition: service_ut.c:1230
Definition: client.h:37
M0_INTERNAL void sim_timer_add(struct sim *state, sim_time_t delta, sim_call_t *cfunc, void *datum)
Definition: sim.c:189
M0_INTERNAL void m0_stob_id_make(uint64_t container, uint64_t key, const struct m0_fid *dom_id, struct m0_stob_id *stob_id)
Definition: stob.c:343
M0_INTERNAL void sim_sleep(struct sim_thread *thread, sim_time_t nap)
Definition: sim.c:372
M0_INTERNAL void sim_chan_wait(struct sim_chan *chan, struct sim_thread *thread)
Definition: sim.c:411
M0_INTERNAL unsigned long long sim_rnd(unsigned long long a, unsigned long long b)
Definition: sim.c:471
unsigned long cl_cached
Definition: client.h:40
M0_INTERNAL void * sim_alloc(size_t size)
Definition: sim.c:85
unsigned long long cwe_offset
Definition: client.c:42
void * sc_datum
Definition: sim.h:138
struct sim * sc_sim
Definition: sim.h:142
struct client_conf * ctp_conf
Definition: client.c:36
unsigned ctp_thread
Definition: client.c:38
m0_bcount_t size
Definition: di.c:39
unsigned cl_id
Definition: client.h:44
static int client_threads_start(struct sim_callout *call)
Definition: client.c:124
M0_INTERNAL void client_fini(struct client_conf *conf)
Definition: client.c:177
unsigned long cwe_count
Definition: client.c:43
M0_TL_DESCR_DEFINE(cl, "client write extents", static, struct client_write_ext, cwe_linkage, cwe_magic, M0_DESIM_CLIENT_WRITE_EXT_MAGIC, M0_DESIM_CLIENT_WRITE_EXT_HEAD_MAGIC)
static struct m0_addb2_source * s
Definition: consumer.c:39
struct sim_chan cl_cache_busy
Definition: client.h:47
Definition: sim.h:287
M0_INTERNAL void cnt_fini(struct cnt *cnt)
Definition: cnt.c:83