summaryrefslogtreecommitdiff
path: root/benchmarks/shared-memory-distribution.c
blob: 6267a5c2c54861fdc71896b987c939424a6f9583 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>

#include <viengoos/activity.h>
#include <hurd/storage.h>
#include <hurd/startup.h>

static vg_addr_t activity;

/* Initialized by the machine-specific startup-code.  */
extern struct hurd_startup_data *__hurd_startup_data;

int
main (int argc, char *argv[])
{
  extern int output_debug;
  output_debug = 1;

  activity = __hurd_startup_data->activity;

#define PAGES 1000

  /* Allocate the buffer.  */
  void *buffer = mmap (0, PAGESIZE * PAGES, PROT_READ | PROT_WRITE,
		       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (buffer == MAP_FAILED)
    {
      printf ("Failed to allocate memory: %s\n", strerror (errno));
      return 1;
    }

#define THREADS 3
  /* And the activities.  */
  vg_addr_t activities[THREADS];

  int i;
  for (i = 0; i < THREADS; i ++)
    activities[i] = storage_alloc (activity, vg_cap_activity,
				   STORAGE_LONG_LIVED,
				   VG_OBJECT_POLICY_DEFAULT,
				   VG_ADDR_VOID).addr;

  bool terminate = false;
  l4_thread_id_t tids[THREADS];
  for (i = 0; i < THREADS; i ++)
    tids[i] = l4_nilthread;

  void *worker (void *arg)
  {
    int w = (intptr_t) arg;

    tids[w] = l4_myself ();

    pthread_setactivity_np (activities[w]);

    uintptr_t t = 0;
    int count = 0;
    while (! terminate)
      {
	int i;
	for (i = 0; i < 1; i ++)
	  {
	    uintptr_t *p = buffer + (rand () % PAGES) * PAGESIZE;
	    t += *p;

	    count ++;

	    if (count % 100000 == 0)
	      debug (0, DEBUG_BOLD ("Read %d pages so far"), count);
	  }

#if 0
	/* ~128Hz.  */
	l4_sleep (l4_time_period (1 << 13));
#elif 1
	l4_thread_switch (tids[rand () % THREADS]);
#endif
      }

    printf ("Thread %d: %d operations\n", w, count);

    /* We need to return t, otherwise, the above loop will be
       optimized away.  */
    return (void *) t;
  }

  /* Start the threads.  */
  pthread_t threads[THREADS];

  error_t err;
  for (i = 0; i < THREADS; i ++)
    {
      err = pthread_create (&threads[i], NULL, worker, (intptr_t) i);
      if (err)
	printf ("Failed to create thread: %s\n", strerror (errno));
    }

#define ITERATIONS 100
  struct vg_activity_stats stats[ITERATIONS][1 + THREADS];

  uintptr_t next_period = 0;
  for (i = 0; i < ITERATIONS; i ++)
    {
      debug (0, DEBUG_BOLD ("starting iteration %d (%x)"), i, l4_myself ());

      struct vg_activity_info info;

      vg_activity_info (activity, activity,
			vg_activity_info_stats, next_period, &info);
      assert (info.event == vg_activity_info_stats);
      assert (info.stats.count > 0);
      if (i != 0)
	assertx (info.stats.stats[0].period != stats[i - 1][0].period,
		 "%x != %x",
		 info.stats.stats[0].period, stats[i - 1][0].period);

      stats[i][0] = info.stats.stats[0];

      int j;
      for (j = 0; j < THREADS; j ++)
	{
	  vg_activity_info (activity, activity,
			    vg_activity_info_stats, next_period, &info);
	  assert (info.event == vg_activity_info_stats);
	  assert (info.stats.count > 0);
	  stats[i][1 + j] = info.stats.stats[0];
	}

      next_period = stats[i][0].period + 1;
    }

  terminate = true;
  for (i = 0; i < THREADS; i ++)
    {
      void *status;
      pthread_join (threads[i], &status);
    }

  printf ("parent ");
  for (i = 0; i < THREADS; i ++)
    printf (VG_ADDR_FMT " ", VG_ADDR_PRINTF (activities[i]));
  printf ("\n");

  for (i = 0; i < ITERATIONS; i ++)
    {
      int j;

      printf ("%d", (int) stats[i][0].period);

      for (j = 0; j < 1 + THREADS; j ++)
	printf ("\t%d\t%d\t%d\t%d\t%d\t%d", (int) stats[i][j].clean,
		(int) stats[i][j].dirty,
		(int) stats[i][j].pending_eviction,
		(int) stats[i][j].discarded,
		(int) stats[i][j].freeloading,
		(int) stats[i][j].freeloaded);
      printf ("\n");
    }

  printf ("Done!\n");

  return 0;
}