-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.c
179 lines (161 loc) · 3.95 KB
/
main.c
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
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* main.c - A CUI for the ZUS daemon
*
* Copyright (c) 2018 NetApp, Inc. All rights reserved.
*
* See module.c for LICENSE details.
*
* Authors:
* Boaz Harrosh <boazh@netapp.com>
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdlib.h>
#include <malloc.h>
#include "zus.h"
#include "zusd.h"
static void usage(int argc, char *argv[])
{
static char msg[] = {
"usage: zus [options] [FILE_PATH]\n"
" --policyRR=[PRIORITY]\n"
" Set threads policy to SCHED_RR.\n"
" Optional PRIORITY is between 1-99. Default is 20\n"
" Only one of --policyRR --policyFIFO or --nice should be\n"
" specified, last one catches\n"
" --policyFIFO=[PRIORITY]\n"
" Set threads policy to SCHED_FIFO.(The default)\n"
" Optional PRIORITY is between 1-99. Default is 20\n"
" Only one of --policyRR --policyFIFO or --nice should be\n"
" specified, last one catches\n"
" --policyFIFO=20 is the default\n"
" --nice=[NICE_VAL]\n"
" Set threads policy to SCHED_OTHER.\n"
" And sets the nice value to NICE_VAL. Default NICE_VAL is 0\n"
" Only one of --policyRR --policyFIFO or --nice should be\n"
" specified, last one catches\n"
" --mlock=[VAL]\n"
" 0 - do not call mlockall.\n"
" 1 - use MCL_CURRENT flag for mlockall.\n"
" 2 - use (MCL_CURRENT | MCL_FUTURE) falgs for mlockall.\n"
" other VAL is same as 0.\n"
"\n"
" FILE_PATH is the path to a mounted zuf-root directory\n"
"\n"
};
FILE *fp = stderr;
int i;
fprintf(fp, "%s", msg);
fprintf(fp, "got: %s ", argv[0]);
for (i = 1; i < argc; ++i)
fprintf(fp, "%s ", argv[i]);
fputs("\n", fp);
}
int main(int argc, char *argv[])
{
struct option opt[] = {
{.name = "policyRR", .has_arg = 2, .flag = NULL, .val = 'r'},
{.name = "policyFIFO", .has_arg = 2, .flag = NULL, .val = 'f'},
{.name = "nice", .has_arg = 2, .flag = NULL, .val = 'n'},
{.name = "verbose", .has_arg = 2, .flag = NULL, .val = 'd'},
{.name = "mlock", .has_arg = 2, .flag = NULL, .val = 'l'},
{.name = "mcheck", .has_arg = 0, .flag = NULL, .val = 'm'},
{.name = "pa_size", .has_arg = 2, .flag = NULL, .val = 'p'},
{.name = 0, .has_arg = 0, .flag = 0, .val = 0},
};
const char *shortopt = "r::f::n::d::l::p::m";
char op;
struct zus_thread_params tp;
const char *path = NULL;
int err, flags = 0;
ssize_t pa_size = 0;
ZTP_INIT(&tp);
while ((op = getopt_long(argc, argv, shortopt, opt, NULL)) != -1) {
switch (op) {
case 'r':
tp.policy = SCHED_RR;
if (optarg)
tp.rr_priority = atoi(optarg);
break;
case 'f':
tp.policy = SCHED_FIFO;
if (optarg)
tp.rr_priority = atoi(optarg);
break;
case 'n':
tp.policy = SCHED_OTHER;
if (optarg)
tp.rr_priority = atoi(optarg);
break;
case 'd':
if (optarg)
g_DBGMASK = strtol(optarg, NULL, 0);
else
g_DBGMASK = 0x1;
break;
case 'l':
if (optarg)
g_mlock = atoi(optarg);
break;
case 'm':
mallopt(M_CHECK_ACTION, 3);
break;
case 'p':
if (optarg)
pa_size = atol(optarg);
break;
default:
/* Just ignore we are not the police */
break;
}
}
argc -= optind;
argv += optind;
if ((argc < 0) || (argc > 1)) {
usage(argc + optind, argv - optind);
return 1;
} else if (argc == 1) {
path = argv[0];
}
switch (g_mlock) {
case MLOCK_ALL: {
flags = MCL_CURRENT | MCL_FUTURE;
break;
}
case MLOCK_CURRENT: {
flags = MCL_CURRENT;
break;
}
case MLOCK_NONE:
default: {
INFO("--mlock=0 is set, potential pagefault deadlock!\n");
break;
}
}
if (flags) {
err = mlockall(flags);
if (unlikely(err))
return err;
}
zus_register_sigactions();
err = zus_increase_max_files();
if (unlikely(err))
return err;
err = zus_setup_pa_size(pa_size);
if (unlikely(err))
return err;
err = zus_slab_init();
if (unlikely(err))
return err;
err = zus_mount_thread_start(&tp, path);
if (unlikely(err))
goto stop;
DBG("waiting for sigint ...\n");
zus_join();
stop:
zus_mount_thread_stop();
return err;
}