-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptions.go
133 lines (117 loc) · 2.56 KB
/
options.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
package cupti
import (
context "context"
)
type Options struct {
ctx context.Context
samplingPeriod int
activities []string
domains []string
callbacks []string
events []string
metrics []string
}
type Option func(o *Options)
func Context(ctx context.Context) Option {
return func(o *Options) {
o.ctx = ctx
}
}
func Activities(activities []string) Option {
if activities == nil {
activities = []string{}
}
return func(o *Options) {
o.activities = strUnion(activities)
}
}
func Domains(domains []string) Option {
if domains == nil {
domains = []string{}
}
return func(o *Options) {
o.domains = strUnion(domains)
}
}
func Callbacks(callbacks []string) Option {
if callbacks == nil {
callbacks = []string{}
}
return func(o *Options) {
o.callbacks = strUnion(callbacks)
}
}
func Events(events []string) Option {
if events == nil {
events = []string{}
}
return func(o *Options) {
o.events = strUnion(events)
if len(o.events) == 0 {
return
}
// maybe this is too automated, but we depend on the following
// callbacks to run for events to work
extraCallbacks := []string{
"CUPTI_CBID_RESOURCE_CONTEXT_CREATED",
"CUPTI_CBID_RESOURCE_CONTEXT_DESTROY_STARTING",
}
o.callbacks = strUnion(append(o.callbacks, extraCallbacks...))
}
}
func Metrics(metrics []string) Option {
if metrics == nil {
metrics = []string{}
}
return func(o *Options) {
o.metrics = strUnion(metrics)
if len(o.metrics) == 0 {
return
}
// maybe this is too automated, but we depend on the following
// callbacks to run for events to work
extraCallbacks := []string{
"CUPTI_CBID_RESOURCE_CONTEXT_CREATED",
"CUPTI_CBID_RESOURCE_CONTEXT_DESTROY_STARTING",
}
o.callbacks = strUnion(append(o.callbacks, extraCallbacks...))
}
}
func SamplingPeriod(s int) Option {
return func(o *Options) {
o.samplingPeriod = s
}
}
func NewOptions(opts ...Option) *Options {
Config.Wait()
options := &Options{}
defaultOptions := []Option{
Context(context.Background()),
SamplingPeriod(Config.SamplingPeriod),
Activities(Config.Activities),
Domains(Config.Domains),
Callbacks(Config.Callbacks),
Events(Config.Events),
Metrics(Config.Metrics),
}
for _, o := range append(defaultOptions, opts...) {
o(options)
}
return options
}
func strUnion(inputs []string) []string {
res := make([]string, 0, len(inputs))
for _, input := range inputs {
found := false
for _, r := range res {
if r == input {
found = true
break
}
}
if !found {
res = append(res, input)
}
}
return res
}