summaryrefslogtreecommitdiffstats
path: root/src/util-profiling-locks.c
blob: 00f9ef652a3086f8ab63d9a13110b84aff67b83b (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* Copyright (C) 2007-2012 Open Information Security Foundation
 *
 * You can copy, redistribute or modify this Program under the terms of
 * the GNU General Public License version 2 as published by the Free
 * Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/**
 * \file
 *
 * \author Victor Julien <victor@inliniac.net>
 *
 * An API for profiling locks.
 *
 */

#include "suricata-common.h"
#include "util-profiling-locks.h"

#ifdef PROFILING
#ifdef PROFILE_LOCKING
#include "threads.h"
#include "util-hashlist.h"
#include "util-debug.h"

thread_local ProfilingLock locks[PROFILING_MAX_LOCKS];
thread_local int locks_idx = 0;
thread_local int record_locks = 0;

int profiling_locks_enabled = 0;
int profiling_locks_output_to_file = 0;
char *profiling_locks_file_name = NULL;
const char *profiling_locks_file_mode = "a";

typedef struct LockRecord_ {
    char *file; // hash

    char *func; // info
    int type;   // info

    int line;   // hash

    uint32_t cont;
    uint32_t ticks_cnt;
    uint64_t ticks_total;
    uint64_t ticks_max;
} LockRecord;

HashListTable *lock_records;
pthread_mutex_t lock_records_mutex;

static uint32_t LockRecordHash(HashListTable *ht, void *buf, uint16_t buflen)
{
     LockRecord *fn = (LockRecord *)buf;
     uint32_t hash = strlen(fn->file) + fn->line;
     uint16_t u;

     for (u = 0; u < strlen(fn->file); u++) {
         hash += fn->file[u];
     }

     return hash % ht->array_size;
}

static char LockRecordCompare(void *buf1, uint16_t len1, void *buf2, uint16_t len2)
{
    LockRecord *fn1 = (LockRecord *)buf1;
    LockRecord *fn2 = (LockRecord *)buf2;

    if (fn1->line != fn2->line)
        return 0;

    if (fn1->file == fn2->file)
        return 1;

    return 0;
}

static void LockRecordFree(void *data)
{
    LockRecord *fn = (LockRecord *)data;

    if (fn == NULL)
        return;
    SCFree(fn);
}

int LockRecordInitHash(void)
{
    pthread_mutex_init(&lock_records_mutex, NULL);
    pthread_mutex_lock(&lock_records_mutex);

    lock_records = HashListTableInit(512, LockRecordHash, LockRecordCompare, LockRecordFree);
    BUG_ON(lock_records == NULL);

    pthread_mutex_unlock(&lock_records_mutex);

    return 0;
}

static void LockRecordAdd(ProfilingLock *l)
{
    LockRecord fn = { NULL, NULL, 0,0,0,0,0,0}, *ptr = &fn;
    fn.file = l->file;
    fn.line = l->line;

    LockRecord *lookup_fn = (LockRecord *)HashListTableLookup(lock_records, (void *)ptr, 0);
    if (lookup_fn == NULL) {
        LockRecord *new = SCMalloc(sizeof(LockRecord));
        BUG_ON(new == NULL);

        new->file = l->file;
        new->line = l->line;
        new->type = l->type;
        new->cont = l->cont;
        new->func = l->func;
        new->ticks_max = l->ticks;
        new->ticks_total = l->ticks;
        new->ticks_cnt = 1;

        HashListTableAdd(lock_records, (void *)new, 0);
    } else {
        lookup_fn->ticks_total += l->ticks;
        if (l->ticks > lookup_fn->ticks_max)
            lookup_fn->ticks_max = l->ticks;
        lookup_fn->ticks_cnt++;
        lookup_fn->cont += l->cont;
    }

    return;
}

/** \param p void ptr to Packet struct */
void SCProfilingAddPacketLocks(void *p)
{
    int i;

    if (profiling_locks_enabled == 0)
        return;

    for (i = 0; i < locks_idx; i++) {
        pthread_mutex_lock(&lock_records_mutex);
        LockRecordAdd(&locks[i]);
        pthread_mutex_unlock(&lock_records_mutex);
    }
}

static void SCProfilingListLocks(void)
{
    FILE *fp = NULL;

    if (profiling_locks_output_to_file == 1) {
        fp = fopen(profiling_locks_file_name, profiling_locks_file_mode);

        if (fp == NULL) {
            SCLogError("failed to open %s: %s", profiling_locks_file_name, strerror(errno));
            return;
        }
    } else {
       fp = stdout;
    }

    fprintf(fp, "\n\nLock                                               Cnt        Avg ticks Max ticks    Total ticks  Cont    Func\n");
    fprintf(fp,     "------------------                                 ---------- --------- ------------ ------------ ------- ---------\n");

    uint64_t total = 0;
    uint32_t cont = 0;
    uint64_t cnt = 0;

    HashListTableBucket *b = HashListTableGetListHead(lock_records);
    while (b) {
        LockRecord *r = HashListTableGetListData(b);

        const char *lock;
        switch (r->type) {
            case LOCK_MUTEX:
                lock = "mtx";
                break;
            case LOCK_SPIN:
                lock = "spn";
                break;
            case LOCK_RWW:
                lock = "rww";
                break;
            case LOCK_RWR:
                lock = "rwr";
                break;
            default:
                lock = "bug";
                break;
        }

        char str[128] = "";
        snprintf(str, sizeof(str), "(%s) %s:%d", lock,r->file, r->line);

        fprintf(fp, "%-50s %-10u %-9"PRIu64" %-12"PRIu64" %-12"PRIu64" %-7u %-s\n",
            str, r->ticks_cnt, (uint64_t)((uint64_t)r->ticks_total/(uint64_t)r->ticks_cnt), r->ticks_max, r->ticks_total, r->cont, r->func);

        total += r->ticks_total;
        cnt += r->ticks_cnt;
        cont += r->cont;

        b = HashListTableGetListNext(b);
    }

    fprintf(fp, "\nOverall: locks %"PRIu64", average cost %"PRIu64", contentions %"PRIu32", total ticks %"PRIu64"\n",
        cnt, (uint64_t)((uint64_t)total/(uint64_t)cnt), cont, total);

    fclose(fp);
}

void LockRecordFreeHash(void)
{
    if (profiling_locks_enabled == 0)
        return;

    pthread_mutex_lock(&lock_records_mutex);

    SCProfilingListLocks();

    if (lock_records != NULL) {
        HashListTableFree(lock_records);
        lock_records = NULL;
    }
    pthread_mutex_unlock(&lock_records_mutex);

    pthread_mutex_destroy(&lock_records_mutex);
}

#endif
#endif