📄 daemon.c
字号:
/* Copyright (C) 2004,2005,2006 Bull S.A. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#if HAVE_CONFIG_H#include <config.h>#endif#define _XOPEN_SOURCE 500#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <limits.h>#include <errno.h>#include <sys/times.h>#include <sys/types.h>#include <sys/wait.h>#include "daemon.h"#include "types.h"#include "write_bin.h"#include "debug.h"#define PTT_SIZE (ptt_buf->size_buff)#define TIMEOUT 0xfffff#define YIELD_TIMEOUT 10extern struct ptt_buffer * ptt_buf;int daemon (ptt_header_t *header,char *binary) { unsigned int end_read = 0; size_t n; int status; unsigned int timeout = TIMEOUT; int pgid = getpgid (0);#ifdef PTT_TRACE_INFO int nb_yield = 0, nb_loop = 0, nb_wait = 0, buf_per_cent = 0; int cur_buf, buf_max = 0; long cpu_clock; struct tms usage;#endif /* PTT_TRACE_INFO */ /* while the user function is running */ while (waitpid (-pgid, &status, WNOHANG) != -1) {#ifdef PTT_TRACE_INFO nb_loop++;#endif /* PTT_TRACE_INFO */ /* when more than half of the array is full, empty it */ /* problem when back to zero */ /* if ((ptt_buf->reserved - ptt_buf->beg_read) >= PTT_SIZE/2) */ if (((ptt_buf->reserved >= ptt_buf->beg_read) && ((ptt_buf->reserved - ptt_buf->beg_read) >= PTT_SIZE/2)) || ((ptt_buf->reserved < ptt_buf->beg_read) && (ptt_buf->reserved + (UINT_MAX - ptt_buf->beg_read) >= PTT_SIZE/2) )) { /* block writters processes to block reservation in the array */ ptt_buf->PAUSE = 1; /* wait for the end of the writting and get it */ while ((ptt_buf->written != ptt_buf->reserved) && --timeout) { if (timeout < (TIMEOUT - YIELD_TIMEOUT)) sched_yield();#ifdef PTT_TRACE_INFO nb_wait++;#endif /* PTT_TRACE_INFO */ } if (timeout == 0) { PTT_TRACE_INTERNALS (PTT_DBG_ERROR, "the application hanged\n"); write_bin (binary, header, ptt_buf->beg_read, ptt_buf->reserved); close_bin (); abort (); } timeout = TIMEOUT; end_read = ptt_buf->written; /* unblock writters process */ ptt_buf->PAUSE = 0; /* copy new written data, there are between beg_read and * end_read */ n = write_bin (binary, header, ptt_buf->beg_read, end_read); #ifdef PTT_TRACE_INFO /* problem when back to zero */ cur_buf = n * 100.0 / PTT_SIZE; if (cur_buf > buf_max) buf_max = cur_buf; buf_per_cent += cur_buf;#endif /* PTT_TRACE_INFO */ ptt_buf->beg_read += n; } else { struct timespec t1 = {0, 1}; /* ms */ nanosleep (&t1, NULL);#ifdef PTT_TRACE_INFO nb_yield++;#endif /* PTT_TRACE_INFO */ } } if (errno != ECHILD) perror ("Waitpid failed %d\n");#ifdef PTT_TRACE_INFO PTT_TRACE_INTERNALS (PTT_DBG_STAT, "Daemon stat : %d loop, %d yield, " "%d wait, %d buffer_write\n", nb_loop, nb_yield, nb_wait, nb_loop-nb_yield); PTT_TRACE_INTERNALS (PTT_DBG_STAT, "buffer %f %% max %d %%\n", (float)buf_per_cent/(nb_loop-nb_yield), buf_max);#endif /* PTT_TRACE_INFO */ ptt_buf->current_level = 0; n = end_read; /* the user function is finished, this is the last trace_point */ while ((ptt_buf->written != ptt_buf->reserved) && --timeout) sched_yield(); if (!timeout) { PTT_TRACE_INTERNALS (PTT_DBG_ERROR, "Timeout : some trace_trace " "were killed (%u<%u)\n", ptt_buf->written, ptt_buf->reserved); ptt_buf->written = ptt_buf->reserved; } end_read = ptt_buf->written; if (n != end_read) /* copy new written data, there are between beg_read and end_read */ n = write_bin (binary, header, ptt_buf->beg_read, end_read); close_bin (); #ifdef PTT_TRACE_INFO cpu_clock = sysconf (_SC_CLK_TCK); times (&usage); PTT_TRACE_INTERNALS (PTT_DBG_STAT, "Child utime %lu.%.2lu stime " "%lu.%.2lu\n", usage.tms_cutime/cpu_clock, usage.tms_cutime%cpu_clock, usage.tms_cstime/cpu_clock, usage.tms_cstime%cpu_clock); PTT_TRACE_INTERNALS (PTT_DBG_STAT, "Daemon utime %lu.%.2lu stime " "%lu.%.2lu\n", usage.tms_utime/cpu_clock, usage.tms_utime%cpu_clock, usage.tms_stime/cpu_clock, usage.tms_stime%cpu_clock); PTT_TRACE_INTERNALS (PTT_DBG_STAT, "Tracetrace time %llu cpt %u " "(%f call/s, %f clock/call)\n", ptt_buf->stat_time, ptt_buf->stat_nb_trace, (float)(ptt_buf->stat_nb_trace)/ usage.tms_cutime*cpu_clock, (float)(ptt_buf->stat_time)/ ptt_buf->stat_nb_trace); PTT_TRACE_INTERNALS (PTT_DBG_STAT, " nb pause %d nb wait %d " "nb async %d\n", ptt_buf->stat_nb_pause, ptt_buf->stat_nb_wait, ptt_buf->stat_async);#endif /* PTT_TRACE_INFO */ return status; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -