summaryrefslogtreecommitdiffstats
path: root/libdimension/profile.c
blob: 164e0020388c798460bd997be0a63933f3664636 (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
/*************************************************************************
 * Copyright (C) 2010-2011 Tavian Barnes <tavianator@tavianator.com>     *
 *                                                                       *
 * This file is part of The Dimension Library.                           *
 *                                                                       *
 * The Dimension Library is free software; you can redistribute it and/  *
 * or modify it under the terms of the GNU Lesser General Public License *
 * as published by the Free Software Foundation; either version 3 of the *
 * License, or (at your option) any later version.                       *
 *                                                                       *
 * The Dimension Library is distributed in the hope that it will be      *
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty   *
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
 * Lesser General Public License for more details.                       *
 *                                                                       *
 * You should have received a copy of the GNU Lesser General Public      *
 * License along with this program.  If not, see                         *
 * <http://www.gnu.org/licenses/>.                                       *
 *************************************************************************/

/**
 * @file
 * Branch profile accounting.
 */

#include "dimension-internal.h"
#include <pthread.h>
#include <stdio.h>
#include <inttypes.h>

/// Information on one predicted branch.
typedef struct {
  char *location;
  uint64_t predicted, branches;
} dmnsn_branch;

/// Count of mispredicted branches.
static dmnsn_dictionary *dmnsn_profile = NULL;
/// Mutex which protects \c dmnsn_profile.
static pthread_mutex_t dmnsn_profile_mutex = PTHREAD_MUTEX_INITIALIZER;

/// Thread-local count of mispredicted branches.
static pthread_key_t dmnsn_thread_profile;
/// Initialize the thread-specific pointer exactly once.
static pthread_once_t dmnsn_thread_profile_once = PTHREAD_ONCE_INIT;

/// Add thread-specific profile data to the global counts.
static void
dmnsn_profile_globalize(void *ptr)
{
  dmnsn_branch *branch = ptr;
  dmnsn_branch *global = dmnsn_dictionary_at(dmnsn_profile, branch->location);
  if (global) {
    global->predicted += branch->predicted;
    global->branches  += branch->branches;
    dmnsn_free(branch->location);
  } else {
    dmnsn_dictionary_insert(dmnsn_profile, branch->location, branch);
  }
}

/// Destructor function for thread-specific profile data.
static void
dmnsn_delete_thread_profile(void *ptr)
{
  dmnsn_dictionary *thread_profile = ptr;

  dmnsn_lock_mutex(&dmnsn_profile_mutex);
    dmnsn_dictionary_apply(thread_profile, dmnsn_profile_globalize);
  dmnsn_unlock_mutex(&dmnsn_profile_mutex);

  dmnsn_delete_dictionary(thread_profile);
}

/// Initialize the thread-specific pointer.
static void
dmnsn_initialize_thread_profile(void)
{
  dmnsn_key_create(&dmnsn_thread_profile, dmnsn_delete_thread_profile);

  dmnsn_lock_mutex(&dmnsn_profile_mutex);
    dmnsn_profile = dmnsn_new_dictionary(sizeof(dmnsn_branch));
  dmnsn_unlock_mutex(&dmnsn_profile_mutex);
}

/// Get the thread-specific profile data.
static dmnsn_dictionary *
dmnsn_get_thread_profile(void)
{
  dmnsn_once(&dmnsn_thread_profile_once, dmnsn_initialize_thread_profile);
  return pthread_getspecific(dmnsn_thread_profile);
}

/// Set the thread-specific profile data.
static void
dmnsn_set_thread_profile(dmnsn_dictionary *thread_profile)
{
  dmnsn_setspecific(dmnsn_thread_profile, thread_profile);
}

bool
dmnsn_expect(bool result, bool expected, const char *func, const char *file,
             unsigned int line)
{
  int size = snprintf(NULL, 0, "%s:%s:%u", file, func, line) + 1;
  if (size < 1) {
    dmnsn_error("sprintf() failed.");
  }

  char key[size];
  if (snprintf(key, size, "%s:%s:%u", file, func, line) < 0) {
    dmnsn_error("sprintf() failed.");
  }

  dmnsn_dictionary *thread_profile = dmnsn_get_thread_profile();
  if (!thread_profile) {
    thread_profile = dmnsn_new_dictionary(sizeof(dmnsn_branch));
    dmnsn_set_thread_profile(thread_profile);
  }

  dmnsn_branch *branch = dmnsn_dictionary_at(thread_profile, key);
  if (branch) {
    ++branch->branches;
    if (result == expected) {
      ++branch->predicted;
    }
  } else {
    dmnsn_branch new_branch = {
      .location  = dmnsn_strdup(key),
      .predicted = (result == expected) ? 1 : 0,
      .branches  = 1
    };
    dmnsn_dictionary_insert(thread_profile, key, &new_branch);
  }

  return result;
}

static void
dmnsn_print_bad_prediction(void *ptr)
{
  dmnsn_branch *branch = ptr;
  double rate = ((double)branch->predicted)/branch->branches;
  if (rate < 0.75 || branch->branches < 100000) {
    fprintf(stderr,
            "Bad branch prediction: %s: %" PRIu64 "/%" PRIu64 " (%g%%)\n",
            branch->location, branch->predicted, branch->branches, 100.0*rate);
  }

  dmnsn_free(branch->location);
}

DMNSN_DESTRUCTOR static void
dmnsn_print_bad_predictions(void)
{
  dmnsn_dictionary *thread_profile = dmnsn_get_thread_profile();
  if (thread_profile) {
    dmnsn_delete_thread_profile(thread_profile);
    dmnsn_set_thread_profile(NULL);
  }

  dmnsn_lock_mutex(&dmnsn_profile_mutex);
    dmnsn_dictionary_apply(dmnsn_profile, dmnsn_print_bad_prediction);
    dmnsn_delete_dictionary(dmnsn_profile);
    dmnsn_profile = NULL;
  dmnsn_unlock_mutex(&dmnsn_profile_mutex);
}