LCOV - code coverage report
Current view: top level - strace_git - count.c (source / functions) Hit Total Coverage
Test: strace-4.16.0.69.f1ea-dirty Code Coverage Lines: 101 111 91.0 %
Date: 2017-03-18 00:38:52 Functions: 7 8 87.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 58 68 85.3 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
       3                 :            :  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
       4                 :            :  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
       5                 :            :  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
       6                 :            :  * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
       7                 :            :  *                     Linux for s390 port by D.J. Barrow
       8                 :            :  *                    <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
       9                 :            :  * Copyright (c) 2004 Roland McGrath <roland@redhat.com>
      10                 :            :  * Copyright (c) 2006 Dmitry V. Levin <ldv@altlinux.org>
      11                 :            :  * All rights reserved.
      12                 :            :  *
      13                 :            :  * Redistribution and use in source and binary forms, with or without
      14                 :            :  * modification, are permitted provided that the following conditions
      15                 :            :  * are met:
      16                 :            :  * 1. Redistributions of source code must retain the above copyright
      17                 :            :  *    notice, this list of conditions and the following disclaimer.
      18                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      19                 :            :  *    notice, this list of conditions and the following disclaimer in the
      20                 :            :  *    documentation and/or other materials provided with the distribution.
      21                 :            :  * 3. The name of the author may not be used to endorse or promote products
      22                 :            :  *    derived from this software without specific prior written permission.
      23                 :            :  *
      24                 :            :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
      25                 :            :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      26                 :            :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      27                 :            :  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
      28                 :            :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      29                 :            :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      30                 :            :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      31                 :            :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      32                 :            :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      33                 :            :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      34                 :            :  */
      35                 :            : 
      36                 :            : #include "defs.h"
      37                 :            : 
      38                 :            : /* Per-syscall stats structure */
      39                 :            : struct call_counts {
      40                 :            :         /* time may be total latency or system time */
      41                 :            :         struct timeval time;
      42                 :            :         int calls, errors;
      43                 :            : };
      44                 :            : 
      45                 :            : static struct call_counts *countv[SUPPORTED_PERSONALITIES];
      46                 :            : #define counts (countv[current_personality])
      47                 :            : 
      48                 :            : static struct timeval shortest = { 1000000, 0 };
      49                 :            : 
      50                 :            : void
      51                 :      15504 : count_syscall(struct tcb *tcp, const struct timeval *syscall_exiting_tv)
      52                 :            : {
      53                 :            :         struct timeval wtv;
      54                 :      15504 :         struct timeval *tv = &wtv;
      55                 :            :         struct call_counts *cc;
      56                 :            : 
      57         [ -  + ]:      15504 :         if (!scno_in_range(tcp->scno))
      58                 :          0 :                 return;
      59                 :            : 
      60         [ +  + ]:      15504 :         if (!counts)
      61                 :         53 :                 counts = xcalloc(nsyscalls, sizeof(*counts));
      62                 :      15504 :         cc = &counts[tcp->scno];
      63                 :            : 
      64                 :      15504 :         cc->calls++;
      65         [ +  + ]:      15504 :         if (syserror(tcp))
      66                 :       6253 :                 cc->errors++;
      67                 :            : 
      68                 :            :         /* tv = wall clock time spent while in syscall */
      69                 :      15504 :         tv_sub(tv, syscall_exiting_tv, &tcp->etime);
      70                 :            : 
      71                 :            :         /* Spent more wall clock time than spent system time? (usually yes) */
      72         [ +  + ]:      15504 :         if (tv_cmp(tv, &tcp->dtime) > 0) {
      73                 :            :                 static struct timeval one_tick = { -1, 0 };
      74                 :            : 
      75         [ +  + ]:      14232 :                 if (one_tick.tv_sec == -1) {
      76                 :            :                         /* Initialize it.  */
      77                 :            :                         struct itimerval it;
      78                 :            : 
      79                 :         48 :                         memset(&it, 0, sizeof it);
      80                 :         48 :                         it.it_interval.tv_usec = 1;
      81                 :         48 :                         setitimer(ITIMER_REAL, &it, NULL);
      82                 :         48 :                         getitimer(ITIMER_REAL, &it);
      83                 :         48 :                         one_tick = it.it_interval;
      84                 :            : //FIXME: this hack doesn't work (tested on linux-3.6.11): one_tick = 0.000000
      85                 :            : //tprintf(" one_tick.tv_usec:%u\n", (unsigned)one_tick.tv_usec);
      86                 :            :                 }
      87                 :            : 
      88         [ +  + ]:      14232 :                 if (tv_nz(&tcp->dtime))
      89                 :            :                         /* tv = system time spent, if it isn't 0 */
      90                 :      12139 :                         tv = &tcp->dtime;
      91         [ +  - ]:       2093 :                 else if (tv_cmp(tv, &one_tick) > 0) {
      92                 :            :                         /* tv = smallest "sane" time interval */
      93         [ -  + ]:       2093 :                         if (tv_cmp(&shortest, &one_tick) < 0)
      94                 :          0 :                                 tv = &shortest;
      95                 :            :                         else
      96                 :       2093 :                                 tv = &one_tick;
      97                 :            :                 }
      98                 :            :         }
      99         [ +  + ]:      15504 :         if (tv_cmp(tv, &shortest) < 0)
     100                 :        120 :                 shortest = *tv;
     101         [ +  + ]:      15504 :         tv_add(&cc->time, &cc->time, count_wallclock ? &wtv : tv);
     102                 :            : }
     103                 :            : 
     104                 :            : static int
     105                 :      55959 : time_cmp(void *a, void *b)
     106                 :            : {
     107                 :      55959 :         return -tv_cmp(&counts[*((int *) a)].time,
     108                 :      55959 :                        &counts[*((int *) b)].time);
     109                 :            : }
     110                 :            : 
     111                 :            : static int
     112                 :      17479 : syscall_cmp(void *a, void *b)
     113                 :            : {
     114                 :      17479 :         const char *a_name = sysent[*((int *) a)].sys_name;
     115                 :      17479 :         const char *b_name = sysent[*((int *) b)].sys_name;
     116 [ +  + ][ +  + ]:      17479 :         return strcmp(a_name ? a_name : "", b_name ? b_name : "");
     117                 :            : }
     118                 :            : 
     119                 :            : static int
     120                 :      10120 : count_cmp(void *a, void *b)
     121                 :            : {
     122                 :      10120 :         int     m = counts[*((int *) a)].calls;
     123                 :      10120 :         int     n = counts[*((int *) b)].calls;
     124                 :            : 
     125 [ +  + ][ +  + ]:      10120 :         return (m < n) ? 1 : (m > n) ? -1 : 0;
     126                 :            : }
     127                 :            : 
     128                 :            : static int (*sortfun)();
     129                 :            : static struct timeval overhead = { -1, -1 };
     130                 :            : 
     131                 :            : void
     132                 :       9361 : set_sortby(const char *sortby)
     133                 :            : {
     134         [ +  + ]:       9361 :         if (strcmp(sortby, "time") == 0)
     135                 :       9349 :                 sortfun = time_cmp;
     136         [ +  + ]:         12 :         else if (strcmp(sortby, "calls") == 0)
     137                 :          6 :                 sortfun = count_cmp;
     138         [ +  - ]:          6 :         else if (strcmp(sortby, "name") == 0)
     139                 :          6 :                 sortfun = syscall_cmp;
     140         [ #  # ]:          0 :         else if (strcmp(sortby, "nothing") == 0)
     141                 :          0 :                 sortfun = NULL;
     142                 :            :         else {
     143                 :          0 :                 error_msg_and_help("invalid sortby: '%s'", sortby);
     144                 :            :         }
     145                 :       9361 : }
     146                 :            : 
     147                 :          0 : void set_overhead(int n)
     148                 :            : {
     149                 :          0 :         overhead.tv_sec = n / 1000000;
     150                 :          0 :         overhead.tv_usec = n % 1000000;
     151                 :          0 : }
     152                 :            : 
     153                 :            : static void
     154                 :         53 : call_summary_pers(FILE *outf)
     155                 :            : {
     156                 :            :         unsigned int i;
     157                 :            :         int     call_cum, error_cum;
     158                 :            :         struct timeval tv_cum, dtv;
     159                 :            :         double  float_tv_cum;
     160                 :            :         double  percent;
     161                 :         53 :         const char *dashes = "----------------";
     162                 :            :         char    error_str[sizeof(int)*3];
     163                 :            :         int    *sorted_count;
     164                 :            : 
     165                 :         53 :         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
     166                 :            :                 "% time", "seconds", "usecs/call",
     167                 :            :                 "calls", "errors", "syscall");
     168                 :         53 :         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
     169                 :            :                 dashes, dashes, dashes, dashes, dashes, dashes);
     170                 :            : 
     171                 :         53 :         sorted_count = xcalloc(sizeof(int), nsyscalls);
     172                 :         53 :         call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
     173         [ +  + ]:         53 :         if (overhead.tv_sec == -1) {
     174                 :         48 :                 tv_mul(&overhead, &shortest, 8);
     175                 :         48 :                 tv_div(&overhead, &overhead, 10);
     176                 :            :         }
     177         [ +  + ]:      18606 :         for (i = 0; i < nsyscalls; i++) {
     178                 :      18553 :                 sorted_count[i] = i;
     179 [ +  - ][ +  + ]:      18553 :                 if (counts == NULL || counts[i].calls == 0)
     180                 :      18050 :                         continue;
     181                 :        503 :                 tv_mul(&dtv, &overhead, counts[i].calls);
     182                 :        503 :                 tv_sub(&counts[i].time, &counts[i].time, &dtv);
     183                 :        503 :                 call_cum += counts[i].calls;
     184                 :        503 :                 error_cum += counts[i].errors;
     185                 :        503 :                 tv_add(&tv_cum, &tv_cum, &counts[i].time);
     186                 :            :         }
     187                 :         53 :         float_tv_cum = tv_float(&tv_cum);
     188         [ +  - ]:         53 :         if (counts) {
     189         [ +  - ]:         53 :                 if (sortfun)
     190                 :         53 :                         qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
     191         [ +  + ]:      18606 :                 for (i = 0; i < nsyscalls; i++) {
     192                 :            :                         double float_syscall_time;
     193                 :      18553 :                         int idx = sorted_count[i];
     194                 :      18553 :                         struct call_counts *cc = &counts[idx];
     195         [ +  + ]:      18553 :                         if (cc->calls == 0)
     196                 :      18050 :                                 continue;
     197                 :        503 :                         tv_div(&dtv, &cc->time, cc->calls);
     198                 :        503 :                         error_str[0] = '\0';
     199         [ +  + ]:        503 :                         if (cc->errors)
     200                 :         68 :                                 sprintf(error_str, "%u", cc->errors);
     201                 :        503 :                         float_syscall_time = tv_float(&cc->time);
     202                 :        503 :                         percent = (100.0 * float_syscall_time);
     203         [ +  + ]:        503 :                         if (percent != 0.0)
     204                 :        498 :                                    percent /= float_tv_cum;
     205                 :            :                         /* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */
     206                 :        503 :                         fprintf(outf, "%6.2f %11.6f %11lu %9u %9.9s %s\n",
     207                 :            :                                 percent, float_syscall_time,
     208                 :        503 :                                 (long) (1000000 * dtv.tv_sec + dtv.tv_usec),
     209                 :            :                                 cc->calls,
     210                 :        503 :                                 error_str, sysent[idx].sys_name);
     211                 :            :                 }
     212                 :            :         }
     213                 :         53 :         free(sorted_count);
     214                 :            : 
     215                 :         53 :         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
     216                 :            :                 dashes, dashes, dashes, dashes, dashes, dashes);
     217                 :         53 :         error_str[0] = '\0';
     218         [ +  + ]:         53 :         if (error_cum)
     219                 :         30 :                 sprintf(error_str, "%u", error_cum);
     220                 :         53 :         fprintf(outf, "%6.6s %11.6f %11.11s %9u %9.9s %s\n",
     221                 :            :                 "100.00", float_tv_cum, "",
     222                 :            :                 call_cum, error_str, "total");
     223                 :         53 : }
     224                 :            : 
     225                 :            : void
     226                 :         48 : call_summary(FILE *outf)
     227                 :            : {
     228                 :         48 :         unsigned int i, old_pers = current_personality;
     229                 :            : 
     230         [ +  + ]:        192 :         for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
     231         [ +  + ]:        144 :                 if (!countv[i])
     232                 :         91 :                         continue;
     233                 :            : 
     234         [ +  + ]:         53 :                 if (current_personality != i)
     235                 :         10 :                         set_personality(i);
     236         [ +  + ]:         53 :                 if (i)
     237                 :          8 :                         fprintf(outf,
     238                 :            :                                 "System call usage summary for %d bit mode:\n",
     239                 :            :                                 current_wordsize * 8);
     240                 :         53 :                 call_summary_pers(outf);
     241                 :            :         }
     242                 :            : 
     243         [ -  + ]:         48 :         if (old_pers != current_personality)
     244                 :          0 :                 set_personality(old_pers);
     245                 :         48 : }

Generated by: LCOV version 1.12