PadicoTM

Documentation

« back to PM2 home.
bench_PSP.c
Go to the documentation of this file.
1/* bench_PSP.c
2 */
3
4#include "bench_common.h"
5#include <Padico/PSP.h>
6#include <Padico/NetSelector.h>
7#include <Padico/PadicoTM.h>
8#include <Padico/Module.h>
9
10int bench_psp_init(void);
11int bench_psp_run(int argc, char**argv);
12
13PADICO_MODULE_DECLARE(bench_PSP, bench_psp_init, bench_psp_run, NULL);
14
15static char*recv_buffer = NULL;
16static size_t recv_lenbuf = 0;
17static size_t len_offset = 0;
18static marcel_t recv_tid;
19static int num_trips = 0;
21static marcel_mutex_t mutex;
22static marcel_cond_t cond;
23
26
27static inline void my_memcpy(void*_dest, const void*_source, size_t len)
28{
29 const int*source = _source;
30 int*dest = _dest;
31 int chunks = len / sizeof(int);
32 register int i;
33 for(i = 0; i < chunks; i++)
34 {
35 dest[i] = source[i];
36 }
37}
38
39/* init a new benchmark */
40static void bench_psp_post_recv(char*buffer, size_t lenbuf)
41{
42 padico_out(40, "post recv- len=%zd\n", lenbuf);
43 marcel_mutex_lock(&mutex);
45 recv_lenbuf = lenbuf;
46 recv_tid = marcel_self();
47 marcel_cond_broadcast(&cond);
48 marcel_mutex_unlock(&mutex);
49}
50
51static void bench_psp_wait(void)
52{
53 padico_out(50, "entering trips=%d.\n", num_trips);
54 marcel_mutex_lock(&mutex);
55 while(recv_buffer)
56 {
57 padico_out(20, "waiting synchro trips=%d...\n", num_trips);
58 marcel_cond_wait(&cond, &mutex);
59 padico_out(20, "unlocked synchro trips=%d...\n", num_trips);
60 }
61 padico_out(40, "stop\n");
62 recv_lenbuf = 0;
64 marcel_mutex_unlock(&mutex);
65 padico_out(60, "done..\n");
66}
67
68static void bench_psp_send(void*buffer, size_t lenbuf)
69{
70 void*_header;
72 uint32_t*header = (uint32_t*)(_header);
73 header[0] = htonl(lenbuf);
74 header[1] = htonl(num_trips);
75 size_t blen = 0, hlen = 0;
76 if(lenbuf > len_offset)
77 {
78 hlen = len_offset;
79 blen = lenbuf - len_offset;
80 }
81 else
82 {
83 hlen = lenbuf;
84 blen = 0;
85 }
86 memcpy(header + 2, buffer, hlen);
87 if(blen > 0)
88 {
89 (*psp.driver->pack)(psp._status, conn, buffer + len_offset, blen);
90 }
91 (*psp.driver->end_message)(psp._status, conn);
92}
93
94static void bench_psp_handler(const void*_header,
96 void*key,
98 void*token)
99{
100 const uint32_t*header = _header;
101 const uint32_t len = ntohl(header[0]);
102 const uint32_t trips = ntohl(header[1]);
103 marcel_mutex_lock(&mutex);
104 while((!recv_buffer) || (len != recv_lenbuf))
105 {
106 padico_out(10, "Ooops! Waiting in handler- recv_lenbuf=%zu; len=%d;\n",
108 marcel_cond_wait(&cond, &mutex);
109 }
110 size_t blen = 0, hlen = 0;
111 if(len > len_offset)
112 {
113 hlen = len_offset;
114 blen = len - len_offset;
115 }
116 else
117 {
118 hlen = len;
119 blen = 0;
120 }
121 memcpy(recv_buffer, header + 2, hlen);
122 if(blen > 0)
123 {
124 (*pump)(token, recv_buffer + len_offset, blen);
125 }
126 num_trips++;
127 const marcel_t tid = recv_tid;
128 /* recv_tid = NULL; */
130 marcel_cond_broadcast(&cond);
131 marcel_mutex_unlock(&mutex);
132 marcel_yield_to(tid);
133}
134
136{
137 marcel_mutex_init(&mutex, NULL);
138 marcel_cond_init(&cond, NULL);
139
143 const char*component_s = padico_getenv("BENCH_PSP_ADAPTER");
144 puk_component_t component = component_s?puk_component_resolve(component_s) : padico_ns_serial_selector(peer, "default", puk_iface_PadicoSimplePackets());
145 if(component == NULL)
146 {
147 padico_fatal("component is NULL.\n");
148 }
149 puk_instance_t instance = puk_component_instantiate(component);
150 if(instance == NULL)
151 {
152 padico_fatal("failed to instanciate component.\n");
153 }
155
156 fprintf(stderr, "component name = %s\n", component->name);
157
158 padico_string_t c = puk_component_serialize(component);
159 padico_out(puk_verbose_always, "bench_PSP: using component %s\n", padico_string_get(c));
160 padico_string_delete(c);
161
162 size_t h_len = 2*sizeof(uint32_t);
163 (*psp.driver->init)(psp._status, 2, "bench_PSP", &h_len, &bench_psp_handler, NULL);
164 len_offset = h_len - 8;
165 assert(len_offset < h_len);
166 padico_out(puk_verbose_always, "bench_PSP: header = %zu; offset = %zu\n", h_len, len_offset);
167 if(psp.driver->listen)
169 if(psp.driver->connect)
171
172 padico_tm_msleep(500);
173 return 0;
174}
175
176static void psp_server(char*buffer, size_t lenbuf)
177{
178 padico_out(20, "starting- len=%zu...\n", lenbuf);
181 bench_psp_send(buffer, lenbuf);
182 padico_out(20, "done- len=%zu...\n", lenbuf);
183}
184
185static void psp_client(char*buffer, size_t lenbuf)
186{
187 padico_out(20, "starting- len=%zu...\n", lenbuf);
189 bench_psp_send(buffer, lenbuf);
191 padico_out(20, "done- len=%zu...\n", lenbuf);
192}
193
194static const struct bench_desc_s psp_bench =
195{
196 .name = "PSP",
197 .server = &psp_server,
198 .client = &psp_client
199};
200
201int bench_psp_run(int argc, char**argv)
202{
204 return 0;
205}
assert(slab_size > 0)
const const struct ns_rule_bucket_s * key
struct ns_rulebucket_hash_entry_s * source
return NULL
Definition Shm.h:88
static void bench_psp_send(void *buffer, size_t lenbuf)
Definition bench_PSP.c:68
static pthread_cond_t cond
Definition bench_PSP.c:22
static const struct bench_desc_s psp_bench
Definition bench_PSP.c:194
static void my_memcpy(void *_dest, const void *_source, size_t len)
Definition bench_PSP.c:27
static size_t len_offset
Definition bench_PSP.c:17
static pthread_mutex_t mutex
Definition bench_PSP.c:21
static size_t recv_lenbuf
len of above buffer
Definition bench_PSP.c:16
int bench_psp_run(int argc, char **argv)
Definition bench_PSP.c:201
static char * recv_buffer
buffer for receipt
Definition bench_PSP.c:15
static void bench_psp_post_recv(char *buffer, size_t lenbuf)
Definition bench_PSP.c:40
static void psp_server(char *buffer, size_t lenbuf)
Definition bench_PSP.c:176
static void bench_psp_wait(void)
Definition bench_PSP.c:51
static padico_topo_node_t peer
Definition bench_PSP.c:24
static void psp_client(char *buffer, size_t lenbuf)
Definition bench_PSP.c:185
static void bench_psp_handler(const void *_header, padico_topo_node_t from, void *key, padico_psp_pump_t pump, void *token)
Definition bench_PSP.c:94
static pthread_t recv_tid
Definition bench_PSP.c:18
int bench_psp_init(void)
Definition bench_PSP.c:135
static int num_trips
number of messages received
Definition bench_PSP.c:19
static struct puk_receptacle_PadicoSimplePackets_s psp
Definition bench_PSP.c:25
void the_bench(const struct bench_desc_s *b)
struct bench_common_param_s bench_common_param
static char * buffer
puk_component_t component
component used for listening
puk_instance_t instance
the listening bootstrap instance
puk_component_t padico_ns_serial_selector(padico_topo_node_t node, const char *profile, puk_iface_t iface)
static puk_iface_t puk_iface_PadicoSimplePackets(void)
Attach the iface name to its driver.
Definition PSP.h:160
void(* padico_psp_pump_t)(void *token, void *bytes, size_t size)
the function to pump data in PSP callbacks
Definition PSP.h:97
void * padico_psp_connection_t
An outgoing connection object, supposed to be opaque for the user.
Definition PSP.h:108
static void puk_instance_indirect_PadicoSimplePackets(puk_instance_t instance, const char *label, struct puk_receptacle_PadicoSimplePackets_s *receptacle)
Definition PSP.h:160
marcel_t tid
Definition PadicoTM.h:165
void padico_tm_msleep(long msec)
Definition tm-boot.c:384
struct padico_ibv_lz_header_s header
padico_topo_node_t client_node
padico_topo_node_t server_node
const char * name
void(* listen)(void *_instance, padico_topo_node_t node)
listen for connections from given node.
Definition PSP.h:126
padico_psp_connection_t(* new_message)(void *_instance, padico_topo_node_t node, void **sbuf)
begin a new outgoing message
Definition PSP.h:142
void(* connect)(void *_instance, padico_topo_node_t node)
connects a remote node through PSP
Definition PSP.h:134
void(* end_message)(void *_instance, padico_psp_connection_t conn)
terminates an outgoing message
Definition PSP.h:156
void(* pack)(void *_instance, padico_psp_connection_t conn, const char *bytes, size_t size)
add a new segment of data to an outgoing message
Definition PSP.h:150
void(* init)(void *_instance, uint32_t tag, const char *label, size_t *h_size, padico_psp_handler_t handler, void *key)
init a new PSP instance
Definition PSP.h:122
struct padico_psp_driver_s * driver
Definition PSP.h:160