summaryrefslogtreecommitdiff
path: root/worker.c
blob: 8686ec1dea34d11a06d7281e97707c0bf20d6a26 (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
/*
 * Copyright 2018 Anton Khirnov <anton@khirnov.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdlib.h>

#include <stdio.h>
#include <sched.h>

#include "worker.h"

#define ENABLE_WORK_STEAL 1

struct Worker {
    WorkSource *ws;
    unsigned int worker_idx;
    int cpu_idx;
    pthread_t thread_id;

    pthread_mutex_t wb_lock;
    atomic_int job_idx_start;
    atomic_int job_idx_end;
    atomic_int queue_empty;

    int batches;
    int steal_races;
};

static void queue_empty(Worker *w)
{
    atomic_store(&w->queue_empty, 1);
    tpi_worksource_queue_empty(w->ws, w->worker_idx);
}

int tpi_worker_steal_job(Worker *w)
{
    int ret = -1;

#if ENABLE_WORK_STEAL
    int job_idx_start, job_idx_end;

    if (atomic_load(&w->queue_empty))
        return -1;

    //pthread_mutex_lock(&w->wb_lock);

    job_idx_end   = atomic_fetch_add(&w->job_idx_end, -1);
    job_idx_start = atomic_load(&w->job_idx_start);

    if (job_idx_start < job_idx_end) {
        ret = job_idx_end - 1;
        if (job_idx_start + 1 >= job_idx_end)
            queue_empty(w);
    } else
        atomic_fetch_add(&w->job_idx_end, 1);

    //pthread_mutex_unlock(&w->wb_lock);
#endif

    return ret;
}

static int worker_get_next_job(Worker *w)
{
    int ret = -1;

#if ENABLE_WORK_STEAL
    while (!atomic_load(&w->queue_empty)) {
        int job_idx_start, job_idx_end;

        job_idx_start = atomic_fetch_add(&w->job_idx_start, 1);
        job_idx_end   = atomic_load(&w->job_idx_end);

        if (job_idx_start < job_idx_end) {
            if (job_idx_start + 1 >= job_idx_end)
                queue_empty(w);
            return job_idx_start;
        }

        // we got preempted by a thief, unroll our changes and try again
        atomic_fetch_add(&w->job_idx_start, -1);
        w->steal_races++;
    }

    if (ret < 0)
        ret = tpi_worksource_steal_job(w->ws, w->worker_idx);
#else
    if (w->job_idx_start < w->job_idx_end)
        return w->job_idx_start++;
    queue_empty(w);
#endif

    return ret;
}

static void *worker_thread(void *arg)
{
    Worker *w = arg;
    int ret;

    if (w->cpu_idx >= 0) {
        cpu_set_t cpumask;

        CPU_ZERO(&cpumask);
        CPU_SET(w->cpu_idx, &cpumask);

        ret = sched_setaffinity(0, sizeof(cpumask), &cpumask);
        if (ret != 0)
            fprintf(stderr, "setaffinity(%d) failed\n", w->cpu_idx);
    }

    while (1) {
        WorkBatch wb;

        ret = tpi_worksource_wait_jobs(w->ws, w->worker_idx, &wb);
        if (ret < 0 || wb.finish)
            break;

        w->batches++;

        atomic_store(&w->job_idx_start, wb.job_idx_start);
        atomic_store(&w->job_idx_end,   wb.job_idx_end);
        atomic_store(&w->queue_empty, 0);

        while ((ret = worker_get_next_job(w)) >= 0)
            wb.job_func(wb.func_arg, ret, w->worker_idx);
    }

    fprintf(stderr, "%d %d batches %d (%2.2f) steal races\n", w->worker_idx, w->batches, w->steal_races, (double)w->batches / w->steal_races );

    return NULL;
}

int tpi_worker_spawn(Worker **pw, unsigned int worker_idx, WorkSource *ws, int cpu_idx)
{
    Worker *w;
    int ret;

    w = calloc(1, sizeof(*w));
    if (!w)
        return -ENOMEM;

    ret = pthread_mutex_init(&w->wb_lock, NULL);
    if (ret != 0) {
        ret = -ret;
        goto fail_free;
    }

    w->worker_idx = worker_idx;
    w->ws         = ws;
    w->cpu_idx    = cpu_idx;

    atomic_store(&w->job_idx_start, -1);
    atomic_store(&w->job_idx_end,   -1);
    atomic_store(&w->queue_empty, 1);

    ret = pthread_create(&w->thread_id, NULL, worker_thread, w);
    if (ret != 0) {
        ret = -ret;
        goto fail_free;
    }

    *pw = w;
    return 0;
fail_free:
    free(w);
    return ret;
}

void tpi_worker_destroy(Worker **pw)
{
    Worker *w = *pw;

    if (!w)
        return;

    pthread_join(w->thread_id, NULL);

    pthread_mutex_destroy(&w->wb_lock);

    free(w);
    *pw = NULL;
}