@@ -51,15 +51,26 @@ namespace ipxp {
5151
5252#define MICRO_SEC 1000000L
5353
54- void input_worker (InputPlugin *plugin, PacketBlock *pkts, size_t block_cnt, uint64_t pkt_limit, ipx_ring_t *queue ,
54+ void input_storage_worker (InputPlugin *plugin, StoragePlugin *cache, PacketBlock *pkts, size_t block_cnt, uint64_t pkt_limit,
5555 std::promise<WorkerResult> *out, std::atomic<InputStats> *out_stats)
5656{
57- struct timespec start;
58- struct timespec end;
57+ struct timespec start_cache;
58+ struct timespec end_cache;
59+ struct timespec begin = {0 , 0 };
60+ struct timespec end = {0 , 0 };
61+ struct timeval ts = {0 , 0 };
62+ bool timeout = false ;
5963 size_t i = 0 ;
6064 InputPlugin::Result ret;
6165 InputStats stats = {0 , 0 , 0 , 0 , 0 };
6266 WorkerResult res = {false , " " };
67+
68+ #ifdef __linux__
69+ const clockid_t clk_id = CLOCK_MONOTONIC_COARSE;
70+ #else
71+ const clockid_t clk_id = CLOCK_MONOTONIC;
72+ #endif
73+
6374 while (!terminate_input) {
6475 PacketBlock *block = &pkts[i];
6576 block->cnt = 0 ;
@@ -79,24 +90,40 @@ void input_worker(InputPlugin *plugin, PacketBlock *pkts, size_t block_cnt, uint
7990 break ;
8091 }
8192 if (ret == InputPlugin::Result::TIMEOUT) {
93+ clock_gettime (clk_id, &end);
94+ if (!timeout) {
95+ timeout = true ;
96+ begin = end;
97+ }
98+ struct timespec diff = {end.tv_sec - begin.tv_sec , end.tv_nsec - begin.tv_nsec };
99+ if (diff.tv_nsec < 0 ) {
100+ diff.tv_nsec += 1000000000 ;
101+ diff.tv_sec --;
102+ }
103+ cache->export_expired (ts.tv_sec + diff.tv_sec );
82104 usleep (1 );
83105 continue ;
84106 } else if (ret == InputPlugin::Result::PARSED) {
85107 stats.packets = plugin->m_seen ;
86108 stats.parsed = plugin->m_parsed ;
87109 stats.dropped = plugin->m_dropped ;
88110 stats.bytes += block->bytes ;
89- #ifdef __linux__
90- const clockid_t clk_id = CLOCK_MONOTONIC_COARSE;
91- #else
92- const clockid_t clk_id = CLOCK_MONOTONIC;
93- #endif
94- clock_gettime (clk_id, &start);
95- ipx_ring_push (queue, static_cast <void *>(block));
96- clock_gettime (clk_id, &end);
111+ clock_gettime (clk_id, &start_cache);
112+ try {
113+ for (unsigned i = 0 ; i < block->cnt ; i++) {
114+ cache->put_pkt (block->pkts [i]);
115+ }
116+ ts = block->pkts [block->cnt - 1 ].ts ;
117+ } catch (PluginError &e) {
118+ res.error = true ;
119+ res.msg = e.what ();
120+ break ;
121+ }
122+ timeout = false ;
123+ clock_gettime (clk_id, &end_cache);
97124
98- int64_t time = end .tv_nsec - start .tv_nsec ;
99- if (start .tv_sec != end .tv_sec ) {
125+ int64_t time = end_cache .tv_nsec - start_cache .tv_nsec ;
126+ if (start_cache .tv_sec != end_cache .tv_sec ) {
100127 time += 1000000000 ;
101128 }
102129 stats.qtime += time;
@@ -116,53 +143,6 @@ void input_worker(InputPlugin *plugin, PacketBlock *pkts, size_t block_cnt, uint
116143 stats.parsed = plugin->m_parsed ;
117144 stats.dropped = plugin->m_dropped ;
118145 out_stats->store (stats);
119- out->set_value (res);
120- }
121-
122- void storage_worker (StoragePlugin *cache, ipx_ring_t *queue, std::promise<WorkerResult> *out)
123- {
124- WorkerResult res = {false , " " };
125- bool timeout = false ;
126- struct timeval ts = {0 , 0 };
127- struct timespec begin = {0 , 0 };
128- struct timespec end = {0 , 0 };
129- #ifdef __linux__
130- const clockid_t clk_id = CLOCK_MONOTONIC_COARSE;
131- #else
132- const clockid_t clk_id = CLOCK_MONOTONIC;
133- #endif
134- while (1 ) {
135- PacketBlock *block = static_cast <PacketBlock *>(ipx_ring_pop (queue));
136- if (block) {
137- try {
138- for (unsigned i = 0 ; i < block->cnt ; i++) {
139- cache->put_pkt (block->pkts [i]);
140- }
141- ts = block->pkts [block->cnt - 1 ].ts ;
142- } catch (PluginError &e) {
143- res.error = true ;
144- res.msg = e.what ();
145- break ;
146- }
147- timeout = false ;
148- } else if (terminate_storage && !ipx_ring_cnt (queue)) {
149- break ;
150- } else {
151- clock_gettime (clk_id, &end);
152- if (!timeout) {
153- timeout = true ;
154- begin = end;
155- }
156- struct timespec diff = {end.tv_sec - begin.tv_sec , end.tv_nsec - begin.tv_nsec };
157- if (diff.tv_nsec < 0 ) {
158- diff.tv_nsec += 1000000000 ;
159- diff.tv_sec --;
160- }
161- cache->export_expired (ts.tv_sec + diff.tv_sec );
162- usleep (1 );
163- }
164- }
165-
166146 cache->finish ();
167147 auto outq = cache->get_queue ();
168148 while (ipx_ring_cnt (outq)) {
0 commit comments