HPCToolkit
stress.c
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 #include <stdio.h>
48 #include <stdlib.h>
49 
50 #include <unistd.h>
51 
52 /* based off of some code by Paul Drongowski */
53 
54 #define KILOBYTE 1024
55 
56 int stride = 128;
57 long chases;
58 unsigned char *chase_array;
59 
60 void
62 {
63  long i, half;
64  unsigned char **p, **p2;
65  long stride8;
66 
67  chase_array = (unsigned char *)malloc(bytes);
68 
69  chases = bytes/stride;
70  half = chases/2;
71  stride8 = stride/8;
72  p = (unsigned char **)&chase_array[0];
73  p2 = (unsigned char **)&chase_array[bytes/2];
74 
75  for(i=0; i<half; ++i) {
76  *p = (unsigned char *) p2;
77  *p2 = (unsigned char *) (p + stride8);
78  p += stride8;
79  p2 += stride8;
80  }
81 
82  *(p2 - stride8) = (unsigned char *)0;
83 }
84 
85 void
86 pointer_chase(long iterations, long kilobytes)
87 {
88  long i;
89 
91 
92  for(i=0; i<iterations; ++i) {
93  unsigned char **p = (unsigned char **)chase_array;
94 
95  while(p = (unsigned char **) *p);
96  }
97 
99 }
100 
101 /* allocate a bunch of memory and wander through it */
102 void
103 memory_stride(long iterations, long kb_to_alloc)
104 {
105  long pagesize = sysconf(_SC_PAGE_SIZE);
106  long kb_per_page = pagesize / KILOBYTE;
107  long bytes = kb_to_alloc * KILOBYTE;
108  char *memhunk = (char *)malloc(bytes);
109 
110  printf("Striding through %ld KB with %ld iterations\n",
111  kb_to_alloc, iterations);
112 
113  /* stride through the memory */
114  for(; --iterations; ) {
115  long i;
116 
117  for(i=0; i<bytes; i+= pagesize) {
118  memhunk[i] = 'f';
119  }
120  }
121 
122  free(memhunk);
123 }
124 
125 int
126 main(int argc, char **argv)
127 {
128  long kilobytes;
129  long iterations;
130  long junk;
131 
132  if(argc != 3) {
133  printf("Usage: %s size(KB) iter\n", argv[0]);
134  exit(1);
135  }
136 
137  kilobytes = atol(argv[1]);
138  iterations = atol(argv[2]);
139 
140  pointer_chase(iterations, kilobytes);
141 
142  return 0;
143 }
int stride
Definition: stress.c:56
void MONITOR_EXT_WRAP_NAME() free(void *ptr)
int main(int argc, char **argv)
Definition: stress.c:126
unsigned char * chase_array
Definition: stress.c:58
#define KILOBYTE
Definition: stress.c:54
void initialize_pointer_chase(long bytes)
Definition: stress.c:61
exit
Definition: names.cpp:1
long chases
Definition: stress.c:57
static size_t pagesize
Definition: mem.c:84
void *MONITOR_EXT_WRAP_NAME() malloc(size_t bytes)
void pointer_chase(long iterations, long kilobytes)
Definition: stress.c:86
void memory_stride(long iterations, long kb_to_alloc)
Definition: stress.c:103