summaryrefslogtreecommitdiffstats
path: root/src/framework/ipc/ps/nsfw_fd_timer.c
blob: fc7174870d218c013720ab8815f6950e27e76206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <sys/timerfd.h>

#include "types.h"
#include "list.h"

#include "nstack_securec.h"
#include "nsfw_init_api.h"
#include "nsfw_mgr_com_api.h"
#include "nsfw_mem_api.h"
#include "nstack_log.h"
#include "nsfw_base_linux_api.h"
#include "nsfw_fd_timer_api.h"
#include "nsfw_maintain_api.h"

#ifdef __cplusplus
/* *INDENT-OFF* */
extern "C"{
/* *INDENT-ON* */
#endif /* __cplusplus */

#define NSFW_TIMER_CYCLE 1
#define NSFW_TIMER_INFO_MAX_COUNT_DEF 8191
#define NSFW_TIMER_INFO_MAX_COUNT (g_timer_cfg.timer_info_size)

nsfw_timer_init_cfg g_timer_cfg;

u8 g_hbt_switch = FALSE;

/*****************************************************************************
*   Prototype    : nsfw_timer_reg_timer
*   Description  : reg timer with callback function when timeout
*   Input        : u32 timer_type
*                  void* data
*                  nsfw_timer_proc_fun fun
*                  struct timespec time_left
*   Output       : None
*   Return Value : nsfw_timer_info*
*   Calls        :
*   Called By    :
*****************************************************************************/
nsfw_timer_info *nsfw_timer_reg_timer(u32 timer_type, void *data,
                                      nsfw_timer_proc_fun fun,
                                      struct timespec time_left)
{
    nsfw_timer_info *tm_info = NULL;
    if (0 ==
        nsfw_mem_ring_dequeue(g_timer_cfg.timer_info_pool, (void *) &tm_info))
    {
        NSFW_LOGERR("dequeue error]data=%p,fun=%p", data, fun);
        return NULL;
    }

    if (EOK != memset_s(tm_info, sizeof(*tm_info), 0, sizeof(*tm_info)))
    {
        if (0 == nsfw_mem_ring_enqueue(g_timer_cfg.timer_info_pool, tm_info))
        {
            NSFW_LOGERR("enqueue error]data=%p,fun=%p", data, fun);
        }
        NSFW_LOGERR("mem set error]data=%p,fun=%p", data, fun);
        return NULL;
    }

    tm_info->fun = fun;
    tm_info->argv = data;
    tm_info->time_left = time_left;
    tm_info->timer_type = timer_type;
    tm_info->alloc_flag = TRUE;
    /* it can start timer after only finish all timer reg, or else it have multi-thread issue */
    dmm_spin_lock(&g_timer_cfg.timer_lock);
    list_add_tail(&tm_info->node, &g_timer_cfg.timer_head);
    dmm_spin_unlock(&g_timer_cfg.timer_lock);
    return tm_info;
}

/*****************************************************************************
*   Prototype    : nsfw_timer_rmv_timer
*   Description  : stop timer
*   Input        : nsfw_timer_info* tm_info
*   Output       : None
*   Return Value : void
*   Calls        :
*   Called By    :
*****************************************************************************/
void nsfw_timer_rmv_timer(nsfw_timer_info * tm_info)
{
    if (NULL == tm_info)
    {
        NSFW_LOGWAR("tm_info nul");
        return;
    }
    /* it can start timer after only finish all timer reg, or else it have multi-thread issue */
    dmm_spin_lock(&g_timer_cfg.timer_lock);

    if (FALSE == tm_info->alloc_flag)
    {
        dmm_spin_unlock(&g_timer_cfg.timer_lock);
        NSFW_LOGERR("tm_info refree]tm_info=%p,argv=%p,fun=%p", tm_info,
                    tm_info->argv, tm_info->fun);
        return;
    }

    tm_info->alloc_flag = FALSE;
    list_del(&tm_info->node);
    if (0 == nsfw_mem_ring_enqueue(g_timer_cfg.timer_info_pool, tm_info))
    {
        NSFW_LOGERR("tm_info free failed]tm_info=%p,argv=%p,fun=%p", tm_info,
                    tm_info->argv, tm_info->fun);
    }
    dmm_spin_unlock(&g_timer_cfg.timer_lock);
    return;
}

/*****************************************************************************
*   Prototype    : nsfw_timer_exp
*   Description  : timer expire
*   Input        : u64 count
*   Output       : None
*   Return Value : u8
*   Calls        :
*   Called By    :
*****************************************************************************/
void nsfw_timer_exp(u64 count)
{
    nsfw_timer_info *tm_info = NULL;
    struct list_head *tNode;
    struct list_head *tPretNode;

    /* it can start timer after only finish all timer reg, or else it have multi-thread issue */
    dmm_spin_lock(&g_timer_cfg.timer_lock);

    list_for_each_entry(tm_info, tNode, (&g_timer_cfg.timer_head), node)
    {

        tPretNode = tm_info->node.prev;
        if (tm_info->time_left.tv_sec > (long) count * NSFW_TIMER_CYCLE)
        {
            tm_info->time_left.tv_sec -= count * NSFW_TIMER_CYCLE;
            continue;
        }

        list_del(&tm_info->node);
        list_add_tail(&tm_info->node, &g_timer_cfg.exp_timer_head);
        tNode = tPretNode;
    }
    dmm_spin_unlock(&g_timer_cfg.timer_lock);

    u32 i = 0;
    while (!list_empty(&g_timer_cfg.exp_timer_head)
           && i++ < NSFW_TIMER_INFO_MAX_COUNT)
    {
        dmm_spin_lock(&g_timer_cfg.timer_lock);
        tm_info =
            (nsfw_timer_info *) list_get_first(&g_timer_cfg.exp_timer_head);
        if (NULL == tm_info)
        {
            dmm_spin_unlock(&g_timer_cfg.timer_lock);
            continue;
        }
        tm_info->alloc_flag = FALSE;
        list_del(&tm_info->node);
        dmm_spin_unlock(&g_timer_cfg.timer_lock);
        /* here can't lock that it is possible that the tm_info->fun call reg timer or remove timer, which will cause deadlock */
        if (NULL != tm_info->fun)
        {
            (void) tm_info->fun(tm_info->timer_type, tm_info->argv);
        }
        /* only after finish handle tm_info->fun, it can put it into timer_info_pool, avoid that it is applied by other timer */
        if (0 == nsfw_mem_ring_enqueue(g_timer_cfg.timer_info_pool, tm_info))
        {
            NSFW_LOGERR("tm_info free failed]tm_info=%p,argv=%p,fun=%p",
                        tm_info, tm_info->argv, tm_info->fun);
        }

    }

}

/*****************************************************************************
*   Prototype    : nsfw_get_timer_socket
*   Description  : get timer socket from kernel
*   Input        : None
*   Output       : None
*   Return Value : i32
*   Calls        :
*   Called By    :
*****************************************************************************/
i32 nsfw_get_timer_socket()
{

    i32 tfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
    if (tfd == -1)
    {
        NSFW_LOGERR("timerfd_create failed!]errno=%d\n", errno);
        return -1;
    }

    /* close on exec */
    if (-1 == nsfw_set_close_on_exec(tfd))
    {
        (void) nsfw_base_close(tfd);
        NSFW_LOGERR("set exec err]fd=%d, errno=%d", tfd, errno);
        return -1;
    }

    if (timerfd_settime(tfd, 0, &g_timer_cfg.ts, NULL) < 0)
    {
        NSFW_LOGERR("timerfd_settime failed] errno=%d", errno);
        close(tfd);
        return -1;
    }

    return tfd;
}

/*****************************************************************************
*   Prototype    : nsfw_timer_notify_fun
*   Description  : receive timer event from kernel
*   Input        : i32 epfd
*                  i32 fd
*                  u32 events
*   Output       : None
*   Return Value : int
*   Calls        :
*   Called By    :
*****************************************************************************/
int nsfw_timer_notify_fun(i32 epfd, i32 fd, u32 events)
{
    i32 rc;

    if ((events & EPOLLERR) || (events & EPOLLHUP) || (!(events & EPOLLIN)))
    {
        (void) nsfw_base_close(fd);
        NSFW_LOGWAR("timer disconnect!]epfd=%d,timer=%d,event=0x%x", epfd,
                    fd, events);

        (void) nsfw_mgr_unreg_sock_fun(fd);
        i32 timer_fd = nsfw_get_timer_socket();
        if (timer_fd < 0)
        {
            NSFW_LOGERR("get timer_fd faied!]epfd=%d,timer_fd=%d,event=0x%x",
                        epfd, fd, events);
            return FALSE;
        }

        (void) nsfw_mgr_reg_sock_fun(timer_fd, nsfw_timer_notify_fun);
        return TRUE;
    }

    u64 data;
    while (1)
    {
        rc = nsfw_base_read(fd, &data, sizeof(data));
        if (rc == 0)
        {
            NSFW_LOGERR("timer_fd recv 0]timer_fd=%d,errno=%d", fd, errno);
            break;
        }
        else if (rc == -1)
        {
            if (errno == EINTR || errno == EAGAIN)
            {
                break;
            }
            NSMON_LOGERR("timer_fd recv]timer_fd=%d,errno=%d", fd, errno);
            break;
        }

        nsfw_timer_exp(data);
    }

    return TRUE;
}

/*****************************************************************************
*   Prototype    : nsfw_timer_start
*   Description  : reg the timer module to epoll thread
*   Input        : None
*   Output       : None
*   Return Value : u8
*   Calls        :
*   Called By    :
*****************************************************************************/
u8 nsfw_timer_start()
{
    i32 timer_fd = nsfw_get_timer_socket();
    if (timer_fd < 0)
    {
        NSFW_LOGERR("get timer_fd fail");
        return FALSE;
    }

    NSFW_LOGINF("start timer_fd module]timer_fd=%d", timer_fd);
    (void) nsfw_mgr_reg_sock_fun(timer_fd, nsfw_timer_notify_fun);
    return TRUE;
}

/*****************************************************************************
*   Prototype    : nsfw_timer_module_init
*   Description  : module init
*   Input        : void* param
*   Output       : None
*   Return Value : int
*   Calls        :
*   Called By    :
*****************************************************************************/
int nsfw_timer_module_init(void *param)
{
    u32 proc_type = (u32) ((long long) param);
    nsfw_timer_init_cfg *timer_cfg = &g_timer_cfg;
    if (proc_type != NSFW_PROC_CTRL)
    {
        NSFW_LOGINF("ps module init]type=%u", proc_type);
    }

    /* The first expiration should be the same as the cyclic interval. This fixs the bug of skipping one beat */
    timer_cfg->ts.it_interval.tv_sec = NSFW_TIMER_CYCLE;
    timer_cfg->ts.it_interval.tv_nsec = 0;
    timer_cfg->ts.it_value.tv_sec = timer_cfg->ts.it_interval.tv_sec;
    timer_cfg->ts.it_value.tv_nsec = timer_cfg->ts.it_interval.tv_nsec;

    switch (proc_type)
    {
        case NSFW_PROC_MASTER:
            /** For nMaster, we call nsfw_mgr_ep_start() after every other module is inited,
            ** so the first expiration is set to 1 nanosecond for nMaster to bring up nMain ASAP */
            timer_cfg->ts.it_value.tv_sec = 0;
            timer_cfg->ts.it_value.tv_nsec = 1;
        case NSFW_PROC_MAIN:
            (void) NSFW_REG_SOFT_INT(NSFW_DBG_MODE_PARAM, g_hbt_switch, 0, 1);
            break;
        case NSFW_PROC_TOOLS:
        case NSFW_PROC_CTRL:
            break;
        default:
            return 0;
    }

    timer_cfg->timer_info_size = NSFW_TIMER_INFO_MAX_COUNT_DEF;

    nsfw_mem_sppool pmpinfo;
    pmpinfo.enmptype = NSFW_MRING_MPMC;
    pmpinfo.usnum = timer_cfg->timer_info_size;
    pmpinfo.useltsize = sizeof(nsfw_timer_info);
    pmpinfo.isocket_id = NSFW_SOCKET_ANY;
    pmpinfo.stname.entype = NSFW_NSHMEM;
    if (-1 ==
        sprintf_s(pmpinfo.stname.aname, NSFW_MEM_NAME_LENTH, "%s",
                  "MS_TM_INFOPOOL"))
    {
        NSFW_LOGERR("sprintf_s fail");
        return -1;
    }
    timer_cfg->timer_info_pool = nsfw_mem_sp_create(&pmpinfo);

    if (!timer_cfg->timer_info_pool)
    {
        NSFW_LOGERR("alloc timer info pool_err");
        return -1;
    }

    MEM_STAT(NSFW_TIMER_MODULE, pmpinfo.stname.aname, NSFW_NSHMEM,
             nsfw_mem_get_len(timer_cfg->timer_info_pool, NSFW_MEM_SPOOL));

    INIT_LIST_HEAD(&(timer_cfg->timer_head));
    INIT_LIST_HEAD(&(timer_cfg->exp_timer_head));

    dmm_spin_init(&g_timer_cfg.timer_lock);
    (void) nsfw_timer_start();

    return 0;
}

/* *INDENT-OFF* */
NSFW_MODULE_NAME(NSFW_TIMER_MODULE)
NSFW_MODULE_PRIORITY(10)
NSFW_MODULE_DEPENDS(NSFW_MGR_COM_MODULE)
NSFW_MODULE_INIT(nsfw_timer_module_init)
/* *INDENT-ON* */

#ifdef __cplusplus
/* *INDENT-OFF* */
}
/* *INDENT-ON* */
#endif /* __cplusplus */