/
main.go
429 lines (396 loc) · 10.8 KB
/
main.go
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package main
/* Tim Henderson (tadh@case.edu)
*
* Copyright (c) 2015, Tim Henderson, Case Western Reserve University
* Cleveland, Ohio 44106. All Rights Reserved.
*
* This library 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 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301
* USA
*/
import (
"fmt"
"log"
"os"
"os/signal"
"runtime/pprof"
"strings"
"syscall"
)
import (
"github.com/timtadh/data-structures/errors"
"github.com/timtadh/getopt"
)
import (
"github.com/timtadh/sfp/cmd"
"github.com/timtadh/sfp/config"
"github.com/timtadh/sfp/miners"
"github.com/timtadh/sfp/miners/fastmax"
"github.com/timtadh/sfp/miners/graple"
"github.com/timtadh/sfp/miners/musk"
"github.com/timtadh/sfp/miners/ospace"
"github.com/timtadh/sfp/miners/premusk"
"github.com/timtadh/sfp/miners/uniprox"
"github.com/timtadh/sfp/miners/walker"
)
func init() {
cmd.UsageMessage = "sfp --help"
cmd.ExtendedMessage = `
sfp - sample frequent patterns
$ sfp -o <path> --samples=<int> --support=<int> [Global Options] \
<type> [Type Options] <input-path> \
<mode> [Mode Options] \
[<reporter> [Reporter Options]]
Note: You must supply [Global Options] then [<type> [Type Options]] then
[<mode> [Mode Options]] and finally <input-path>. Changes in ordering are
not supported.
Note: You may either supply the <input-path> as a regular file or a gzipped
file. If supplying a gzip file the file extension must be '.gz'.
Note: If you don't supply a reporter by default it will use 'chain log file'.
See the the documentations for Reporters for details.
Global Options
-h, --help view this message
--types show the available types
--modes show the available modes
-o, --ouput=<path> path to output directory (required)
NB: will overwrite contents of dir
-c, --cache=<path> path to cache directory (optional)
NB: will overwrite contents of dir
-p, --parallelism=<int> Parallelism level to use. Defaults to
the number of CPU cores you have. Set to
0 to turn off parallelism.
--samples=<int> number of samples to collect (required)
--support=<int> minimum support of patterns (required)
--non-unique by default, sfp collects only unique samples. This
option allows non-unique samples.
--skip-log=<level> don't output the given log level.
Developer Options
--cpu-profile=<path> write a cpu-profile to this location
heap-profile Reporter
$ sfp ... <type> ... <mode> ... chain ... heap-profile [options]
-p, profile=<path> where you want the heap-profile written
-e, every=<int> collect every n samples collected (default 1)
-a, after=<int> collect after n samples collected (default 0)
Modes
graple the GRAPLE (unweighted random walk) algorithm.
musk uniform sampling of maximal patterns.
ospace uniform sampling of all patterns.
fastmax faster sampling of large max patterns than
graple.
premusk musk but with random teleports
uniprox approximately uniform sampling of max patterns
using an absorbing chain
premusk Options
-t, teleports=<float> the probability of teleporting (default: .01)
uniprox Options
-w, walks=<int> (default 15) number of estimating
walks
`
}
func grapleMode(argv []string, conf *config.Config) (miners.Miner, []string) {
args, optargs, err := getopt.GetOpt(
argv,
"hc",
[]string{
"help",
},
)
if err != nil {
fmt.Fprintln(os.Stderr, err)
cmd.Usage(cmd.ErrorCodes["opts"])
}
for _, oa := range optargs {
switch oa.Opt() {
case "-h", "--help":
cmd.Usage(0)
default:
fmt.Fprintf(os.Stderr, "Unknown flag '%v'\n", oa.Opt())
cmd.Usage(cmd.ErrorCodes["opts"])
}
}
return graple.NewWalker(conf), args
}
func fastmaxMode(argv []string, conf *config.Config) (miners.Miner, []string) {
args, optargs, err := getopt.GetOpt(
argv,
"h",
[]string{
"help",
},
)
if err != nil {
fmt.Fprintln(os.Stderr, err)
cmd.Usage(cmd.ErrorCodes["opts"])
}
for _, oa := range optargs {
switch oa.Opt() {
case "-h", "--help":
cmd.Usage(0)
default:
fmt.Fprintf(os.Stderr, "Unknown flag '%v'\n", oa.Opt())
cmd.Usage(cmd.ErrorCodes["opts"])
}
}
return fastmax.NewWalker(conf), args
}
func uniproxMode(argv []string, conf *config.Config) (miners.Miner, []string) {
args, optargs, err := getopt.GetOpt(
argv,
"hw:",
[]string{
"help",
"walks=",
"max",
},
)
if err != nil {
fmt.Fprintln(os.Stderr, err)
cmd.Usage(cmd.ErrorCodes["opts"])
}
walks := 15
max := false
for _, oa := range optargs {
switch oa.Opt() {
case "-h", "--help":
cmd.Usage(0)
case "-w", "--walks":
walks = cmd.ParseInt(oa.Arg())
case "--max":
max = true
default:
fmt.Fprintf(os.Stderr, "Unknown flag '%v'\n", oa.Opt())
cmd.Usage(cmd.ErrorCodes["opts"])
}
}
miner, err := uniprox.NewWalker(conf, walks, max)
if err != nil {
log.Fatal(err)
}
return miner, args
}
func muskMode(argv []string, conf *config.Config) (miners.Miner, []string) {
args, optargs, err := getopt.GetOpt(
argv,
"h",
[]string{
"help",
},
)
if err != nil {
fmt.Fprintln(os.Stderr, err)
cmd.Usage(cmd.ErrorCodes["opts"])
}
for _, oa := range optargs {
switch oa.Opt() {
case "-h", "--help":
cmd.Usage(0)
default:
fmt.Fprintf(os.Stderr, "Unknown flag '%v'\n", oa.Opt())
cmd.Usage(cmd.ErrorCodes["opts"])
}
}
miner := walker.NewWalker(conf, musk.MakeMaxUniformWalk(musk.Next, nil))
return miner, args
}
func premuskMode(argv []string, conf *config.Config) (miners.Miner, []string) {
args, optargs, err := getopt.GetOpt(
argv,
"h",
[]string{
"help",
"teleport=",
},
)
if err != nil {
fmt.Fprintln(os.Stderr, err)
cmd.Usage(cmd.ErrorCodes["opts"])
}
teleport := .01
for _, oa := range optargs {
switch oa.Opt() {
case "-h", "--help":
cmd.Usage(0)
case "--teleport":
teleport = cmd.ParseFloat(oa.Arg())
default:
fmt.Fprintf(os.Stderr, "Unknown flag '%v'\n", oa.Opt())
cmd.Usage(cmd.ErrorCodes["opts"])
}
}
miner := premusk.NewWalker(conf, teleport)
return miner, args
}
func ospaceMode(argv []string, conf *config.Config) (miners.Miner, []string) {
args, optargs, err := getopt.GetOpt(
argv,
"h",
[]string{
"help",
},
)
if err != nil {
fmt.Fprintln(os.Stderr, err)
cmd.Usage(cmd.ErrorCodes["opts"])
}
for _, oa := range optargs {
switch oa.Opt() {
case "-h", "--help":
cmd.Usage(0)
default:
fmt.Fprintf(os.Stderr, "Unknown flag '%v'\n", oa.Opt())
cmd.Usage(cmd.ErrorCodes["opts"])
}
}
miner := walker.NewWalker(conf, ospace.MakeUniformWalk(0, true))
return miner, args
}
func main() {
code := run()
if code != 0 {
os.Exit(code)
}
}
func run() int {
modes := map[string]cmd.Mode{
"graple": grapleMode,
"fastmax": fastmaxMode,
"musk": muskMode,
"ospace": ospaceMode,
"premusk": premuskMode,
"uniprox": uniproxMode,
}
args, optargs, err := getopt.GetOpt(
os.Args[1:],
"ho:c:p:",
[]string{
"help",
"output=", "cache=",
"modes", "types", "reporters",
"non-unique",
"support=",
"samples=",
"skip-log=",
"cpu-profile=",
"parallelism=",
},
)
if err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, "could not process your arguments (perhaps you forgot a mode?) try:")
fmt.Fprintf(os.Stderr, "$ %v breadth %v\n", os.Args[0], strings.Join(os.Args[1:], " "))
cmd.Usage(cmd.ErrorCodes["opts"])
}
output := ""
cache := ""
unique := true
support := 0
samples := 0
cpuProfile := ""
parallelism := -1
for _, oa := range optargs {
switch oa.Opt() {
case "-h", "--help":
cmd.Usage(0)
case "-o", "--output":
output = cmd.EmptyDir(oa.Arg())
case "-c", "--cache":
cache = cmd.EmptyDir(oa.Arg())
case "-p", "--parallelism":
parallelism = cmd.ParseInt(oa.Arg())
case "--support":
support = cmd.ParseInt(oa.Arg())
case "--samples":
samples = cmd.ParseInt(oa.Arg())
case "--non-unique":
unique = false
case "--types":
fmt.Fprintln(os.Stderr, "Types:")
for k := range cmd.Types {
fmt.Fprintln(os.Stderr, " ", k)
}
os.Exit(0)
case "--modes":
fmt.Fprintln(os.Stderr, "Modes:")
for k := range modes {
fmt.Fprintln(os.Stderr, " ", k)
}
os.Exit(0)
case "--reporters":
fmt.Fprintln(os.Stderr, "Reporters:")
for k := range cmd.Reporters {
fmt.Fprintln(os.Stderr, " ", k)
}
os.Exit(0)
case "--skip-log":
level := oa.Arg()
errors.Logf("INFO", "not logging level %v", level)
errors.SkipLogging[level] = true
case "--cpu-profile":
cpuProfile = cmd.AssertFile(oa.Arg())
default:
fmt.Fprintf(os.Stderr, "Unknown flag '%v'\n", oa.Opt())
cmd.Usage(cmd.ErrorCodes["opts"])
}
}
if support <= 0 {
fmt.Fprintf(os.Stderr, "Support <= 0, must be > 0\n")
cmd.Usage(cmd.ErrorCodes["opts"])
}
if samples <= 0 {
fmt.Fprintf(os.Stderr, "Samples <= 0, must be > 0\n")
cmd.Usage(cmd.ErrorCodes["opts"])
}
if output == "" {
fmt.Fprintf(os.Stderr, "You must supply an output dir (-o)\n")
cmd.Usage(cmd.ErrorCodes["opts"])
}
if cpuProfile != "" {
errors.Logf("DEBUG", "starting cpu profile: %v", cpuProfile)
f, err := os.Create(cpuProfile)
if err != nil {
log.Fatal(err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
log.Fatal(err)
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig:=<-sigs
errors.Logf("DEBUG", "closing cpu profile")
pprof.StopCPUProfile()
err := f.Close()
errors.Logf("DEBUG", "closed cpu profile, err: %v", err)
panic(errors.Errorf("caught signal: %v", sig))
}()
defer func() {
errors.Logf("DEBUG", "closing cpu profile")
pprof.StopCPUProfile()
err := f.Close()
errors.Logf("DEBUG", "closed cpu profile, err: %v", err)
}()
}
conf := &config.Config{
Cache: cache,
Output: output,
Support: support,
Samples: samples,
Unique: unique,
Parallelism: parallelism,
}
return cmd.Main(args, conf, modes)
}