LCOV - code coverage report
Current view: directory - src/trusted/perf_counter - nacl_perf_counter_test.c (source / functions) Found Hit Coverage
Test: coverage.lcov Lines: 54 54 100.0 %
Date: 2014-06-18 Functions: 0 0 -

       1                 : /*
       2                 :  * Copyright (c) 2011 The Native Client Authors. All rights reserved.
       3                 :  * Use of this source code is governed by a BSD-style license that can be
       4                 :  * found in the LICENSE file.
       5                 :  */
       6                 : 
       7                 : #include <stdio.h>
       8                 : 
       9                 : #include "native_client/src/include/nacl_assert.h"
      10                 : #include "native_client/src/include/portability.h"
      11                 : #include "native_client/src/include/portability_io.h"
      12                 : #include "native_client/src/include/portability_string.h"
      13                 : #include "native_client/src/shared/platform/nacl_log.h"
      14                 : #include "native_client/src/shared/platform/nacl_time.h"
      15                 : 
      16                 : #include "native_client/src/trusted/perf_counter/nacl_perf_counter.h"
      17                 : 
      18                 : /* A simple test of the performance counter basics. */
      19                 : 
      20                 : struct perf_counter_test {
      21                 :   struct nacl_abi_timeval _[2];
      22                 :   int64_t res;
      23                 : };
      24                 : 
      25                 : struct perf_counter_test arr[] = {
      26                 :   { { { 0, 0 }, { 0, 0 } }, 0 },
      27                 :   { { { 0, 100 }, { 0, 0 } }, -100 },
      28                 :   { { { 1, 100, }, { 2, 5 } }, (999*1000 + 905) },
      29                 :   { { { 0, 0 }, { 0, 0 } }, 0 },
      30                 : };
      31                 : 
      32                 : 
      33                 : #define ALEN(A) ((int)(sizeof(A)/sizeof(A[0])))
      34                 : 
      35               1 : int main(int argc, char*argv[]) {
      36               1 :   int i = 0;
      37               1 :   int64_t res = 0;
      38               2 :   UNREFERENCED_PARAMETER(argc);
      39               2 :   UNREFERENCED_PARAMETER(argv);
      40               1 :   NaClLogModuleInit();
      41               1 :   NaClTimeInit();
      42                 : 
      43               8 :   for (; i < ALEN(arr)-1; ++i) {
      44               3 :     struct NaClPerfCounter tm;
      45               3 :     memset(&tm, 0, sizeof(struct NaClPerfCounter));
      46                 : 
      47               3 :     strncpy(tm.app_name, "mocked app", ALEN(tm.app_name));
      48                 : 
      49               3 :     tm.sample_list[0] = arr[i]._[0];
      50               3 :     tm.sample_list[1] = arr[i]._[1];
      51               3 :     tm.samples = 2;
      52               3 :     res = NaClPerfCounterInterval(&tm, 0, 1);
      53               9 :     ASSERT_EQ(res, arr[i].res);
      54               3 :   }
      55                 : 
      56               1 :   do {
      57                 :     /* live run */
      58               1 :     unsigned i;
      59               1 :     struct NaClPerfCounter tm;
      60               1 :     NaClPerfCounterCtor(&tm, "test_perf_counter");
      61                 : 
      62               3 :     ASSERT_EQ(1, tm.samples);
      63                 : 
      64              28 :     for (i = 1; i < NACL_MAX_PERF_COUNTER_SAMPLES-2; ++i) {
      65              13 :       NaClPerfCounterMark(&tm, "foo");
      66              39 :       ASSERT_EQ(i+1, tm.samples);
      67              13 :     }
      68               1 :   } while (0);
      69                 : 
      70               1 :   do {
      71                 :     /* live run */
      72               1 :     unsigned i;
      73               1 :     struct NaClPerfCounter tm;
      74               1 :     int r = 0;
      75               1 :     const char *names[] = {
      76                 :       "--",
      77                 :       "first",
      78                 :       "second",
      79                 :       "third",
      80                 :       "fourth",
      81                 :       "fifth",
      82                 :       "sixth",
      83                 :       /* Note: this name must be longer than NACL_MAX_PERF_COUNTER_NAME */
      84                 :       "a-really-long-name-which-will-be-truncated",
      85                 :       "eighth",
      86                 :       "ninth",
      87                 :       "tenth",
      88                 :       "eleventh",
      89                 :       "twelveth",
      90                 :       "thirteenth",
      91                 :       "14th",
      92                 :       "15th",
      93                 :     };
      94               2 :     ASSERT_EQ(NACL_MAX_PERF_COUNTER_SAMPLES, ALEN(names));
      95                 : 
      96               1 :     NaClPerfCounterCtor(&tm, "test_perf_counter");
      97                 : 
      98               3 :     ASSERT_EQ(1, tm.samples);
      99                 : 
     100              32 :     for (i = 1; i < ALEN(names); ++i) {
     101              15 :       NaClLog(1, "NaCl_Perf_Counter_Test: testing event name %d: %s\n",
     102                 :               i, names[i]);
     103              15 :       r = NaClPerfCounterMark(&tm, names[i]);
     104              45 :       ASSERT_EQ(i+1, tm.samples);
     105              45 :       ASSERT_EQ(i, (unsigned)r);
     106              45 :       ASSERT_EQ(0, strncmp(tm.sample_names[i], names[i], \
     107                 :                            (NACL_MAX_PERF_COUNTER_NAME-1)));
     108              15 :     }
     109               1 :     NaClLog(1, "NaCl_Perf_Counter_Test: checking truncated name: %s vs %s\n",
     110                 :             tm.sample_names[7], names[7]);
     111               3 :     ASSERT_EQ(NACL_MAX_PERF_COUNTER_SAMPLES, tm.samples);
     112               3 :     ASSERT_EQ(strlen(tm.sample_names[7]), (NACL_MAX_PERF_COUNTER_NAME-1));
     113                 : 
     114               1 :     NaClLog(1, "NaCl_Perf_Counter_Test: checking event overflow\n");
     115               1 :     r = NaClPerfCounterMark(&tm, "foo");
     116               3 :     ASSERT_EQ(NACL_MAX_PERF_COUNTER_SAMPLES, tm.samples);
     117               3 :     ASSERT_EQ(-1, r);
     118               3 :     ASSERT_NE(-1, NaClPerfCounterInterval(&tm, 0, tm.samples - 1));
     119               1 :   } while (0);
     120                 : 
     121               1 :   NaClTimeFini();
     122               1 :   NaClLogModuleFini();
     123               1 :   return 0;
     124                 : }

Generated by: LCOV version 1.7