1 /*
2 * Copyright (c) International Business Machines Corp., 2002
3 * Copyright (C) 2014 Linux Test Project, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 */
15 /*
16 * ALGORITHM
17 * Set up a profiling buffer, turn profiling on, set a timer for
18 * cpu time, spin the pc and wait for timer to go off.
19 * The profiling buffer should contain some info, highly concentrated.
20 * We just do a "looks reasonable" check.
21 */
22
23 #include <stdio.h>
24 #include <signal.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include "test.h"
29 #include "safe_macros.h"
30
31 #define PROFIL_TIME 5
32
33 /* Should be large enough to hold data for test_profil() .text,
34 * on x86_64 this is ~600 bytes, so 16k should enough for all arches.
35 * We will monitor 16k on each side around current pc value,
36 * just in case compiler put call to get_pc() below "data shuffling" code */
37 #define PROFIL_BUFLEN (32*1024)
38
39 char *TCID = "profil01";
40 int TST_TOTAL = 1;
41
42 static volatile sig_atomic_t profil_done;
43
alrm_handler(int sig)44 static void alrm_handler(int sig)
45 {
46 (void) sig;
47 profil_done = 1;
48 }
49
get_pc(void)50 static void __attribute__ ((noinline)) *get_pc(void)
51 {
52 #if defined(__s390__) && __WORDSIZE == 32
53 /* taken from glibc,
54 * sysdeps/unix/sysv/linux/s390/s390-32/profil-counter.h
55 * 31-bit s390 pointers don't use the 32th bit, however integers do,
56 * so wrap the value around at 31 bits */
57 return (void *)
58 ((unsigned long) __builtin_return_address(0) & 0x7fffffffUL);
59 #else
60 return __builtin_return_address(0);
61 #endif
62 }
63
test_profil(void)64 static void test_profil(void)
65 {
66 unsigned short buf[PROFIL_BUFLEN] = { 0 };
67 volatile int data[8] = { 0 };
68 size_t offset = (size_t) get_pc() - PROFIL_BUFLEN/2, count = 0;
69 int ret, i;
70
71 /* reset for test looping */
72 profil_done = 0;
73
74 /* profil_count in glibc calculates offset as
75 * i = (pc - pc_offset - (void *) 0) / 2
76 * i = i * pc_scale / 65536
77 * set scale to 2*65536 to have 1:1 mapping for $pc */
78 ret = profil(buf, sizeof(buf), offset, 2*65536);
79 if (ret)
80 tst_brkm(TBROK, NULL, "profil returned: %d\n", ret);
81
82 signal(SIGALRM, alrm_handler);
83 alarm(PROFIL_TIME);
84
85 while (!profil_done) {
86 if (data[0])
87 data[0] = -data[7];
88 else
89 data[1] = data[0] / 2;
90 if (data[2])
91 data[2] = data[1] * 2;
92 else
93 data[3] = data[2] + data[0];
94 if (data[4])
95 data[4] = data[3] - data[1];
96 else
97 data[5] = data[4] * data[2];
98 if (data[6])
99 data[6] = data[5] + data[3];
100 else
101 data[7] = data[6] - data[4];
102 }
103
104 for (i = 0; i < PROFIL_BUFLEN; i++)
105 if (buf[i]) {
106 tst_resm(TINFO, "buf[0x%04x]=%d", i, buf[i]);
107 count += buf[i];
108 }
109
110 if (count > 0)
111 tst_resm(TPASS, "profil recorded some data");
112 else
113 tst_resm(TFAIL, "profil failed to record anything");
114 }
115
main(int ac,char * av[])116 int main(int ac, char *av[])
117 {
118 int lc;
119
120 tst_parse_opts(ac, av, NULL, NULL);
121
122 for (lc = 0; TEST_LOOPING(lc); lc++)
123 test_profil();
124
125 tst_exit();
126 }
127