@@ -10,6 +10,7 @@ import (
10
10
"log"
11
11
"os"
12
12
"os/exec"
13
+ "runtime/pprof"
13
14
"strconv"
14
15
"strings"
15
16
"text/tabwriter"
@@ -99,10 +100,22 @@ func initdb() {
99
100
post .Process (db )
100
101
}
101
102
103
+ var cpuprofile = flag .String ("cpuprofile" , "" , "write cpu profile to file" )
104
+
102
105
func main () {
103
106
log .SetFlags (0 )
104
107
flag .Parse ()
105
108
109
+ flag .Parse ()
110
+ if * cpuprofile != "" {
111
+ f , err := os .Create (* cpuprofile )
112
+ if err != nil {
113
+ log .Fatal (err )
114
+ }
115
+ pprof .StartCPUProfile (f )
116
+ defer pprof .StopCPUProfile ()
117
+ }
118
+
106
119
if * help || flag .NArg () < 1 {
107
120
fmt .Println ("Usage: cyan -db <cyclus-db> [flags...] <command> [flags...] [args...]" )
108
121
fmt .Println ("Computes metrics for cyclus simulation data in a sqlite database." )
@@ -136,33 +149,45 @@ func doCustom(cmd string, args ...interface{}) *bytes.Buffer {
136
149
tw := tabwriter .NewWriter (& buf , 4 , 4 , 1 , ' ' , 0 )
137
150
cols , err := rows .Columns ()
138
151
fatalif (err )
139
- for _ , c := range cols {
152
+
153
+ simidcol := - 1
154
+ for i , c := range cols {
155
+ if strings .Contains (strings .ToLower (c ), "simid" ) {
156
+ simidcol = i
157
+ }
140
158
_ , err := tw .Write ([]byte (c + "\t " ))
141
159
fatalif (err )
142
160
}
143
161
_ , err = tw .Write ([]byte ("\n " ))
144
162
fatalif (err )
145
163
164
+ vs := make ([]interface {}, len (cols ))
165
+ vals := make ([]* sql.NullString , len (cols ))
166
+ for i := range vals {
167
+ vals [i ] = & sql.NullString {}
168
+ vs [i ] = vals [i ]
169
+ }
170
+
146
171
for rows .Next () {
147
- vs := make ([]interface {}, len (cols ))
148
- vals := make ([]* sql.NullString , len (cols ))
149
172
for i := range vals {
150
- vals [i ] = & sql.NullString {}
151
- vs [i ] = vals [i ]
173
+ vals [i ].Valid = false
152
174
}
175
+
153
176
err := rows .Scan (vs ... )
154
177
fatalif (err )
155
178
156
179
for i , v := range vals {
157
- s := "NULL"
158
180
if v .Valid {
159
181
s = v .String
160
- if strings . Contains ( strings . ToLower ( cols [ i ]), "simid" ) {
182
+ if i == simidcol {
161
183
s = uuid .UUID (v .String ).String ()
162
184
}
185
+ tw .Write ([]byte (s + "\t " ))
186
+ } else {
187
+ tw .Write ([]byte ("NULL\t " ))
163
188
}
164
- fmt .Fprintf (tw , "%v\t " , s )
165
189
}
190
+
166
191
_ , err = tw .Write ([]byte ("\n " ))
167
192
fatalif (err )
168
193
}
@@ -335,24 +360,36 @@ func doInv(cmd string, args []string) {
335
360
initdb ()
336
361
337
362
proto := fs .Arg (0 )
338
- s := `SELECT tl.Time AS Time,TOTAL(sub.qty) AS Quantity FROM timelist as tl
339
- LEFT JOIN (
340
- SELECT tl.simid AS simid, tl.Time as time,TOTAL(inv.Quantity*c.MassFrac) AS qty
341
- FROM timelist as tl
342
- JOIN inventories as inv on inv.starttime <= tl.time and inv.endtime > tl.time AND tl.simid=inv.simid
343
- JOIN agents as a on a.agentid=inv.agentid AND a.simid=inv.simid
344
- JOIN compositions as c on c.qualid=inv.qualid AND c.simid=inv.simid
345
- WHERE a.simid=? AND a.prototype=? {{.}}
346
- GROUP BY tl.Time
347
- ) AS sub ON sub.time=tl.time AND sub.simid=tl.simid
348
- WHERE tl.simid=?
349
- GROUP BY tl.Time;
350
- `
363
+
364
+ filter := nuclidefilter (* nucs )
365
+ s := ""
366
+ if filter != "" {
367
+ s = `SELECT tl.Time AS Time,IFNULL(sub.qty, 0) FROM timelist as tl
368
+ LEFT JOIN (
369
+ SELECT tl.Time as time,SUM(inv.Quantity*c.MassFrac) AS qty
370
+ FROM timelist as tl
371
+ JOIN inventories as inv on UNLIKELY(inv.starttime <= tl.time AND inv.endtime > tl.time) AND tl.simid=inv.simid
372
+ JOIN agents as a on a.agentid=inv.agentid AND a.simid=inv.simid
373
+ JOIN compositions as c on c.qualid=inv.qualid AND c.simid=inv.simid
374
+ WHERE a.simid=? AND a.prototype=? {{.}}
375
+ GROUP BY tl.Time
376
+ ) AS sub ON sub.time=tl.time WHERE tl.simid=?`
377
+ } else {
378
+ s = `SELECT tl.Time AS Time,IFNULL(sub.qty, 0) FROM timelist as tl
379
+ LEFT JOIN (
380
+ SELECT tl.Time as time,SUM(inv.Quantity) AS qty
381
+ FROM timelist as tl
382
+ JOIN inventories as inv on UNLIKELY(inv.starttime <= tl.time AND inv.endtime > tl.time) AND tl.simid=inv.simid
383
+ JOIN agents as a on a.agentid=inv.agentid AND a.simid=inv.simid
384
+ WHERE a.simid=? AND a.prototype=? {{.}}
385
+ GROUP BY tl.Time
386
+ ) AS sub ON sub.time=tl.time
387
+ WHERE tl.simid=?`
388
+ }
351
389
352
390
tmpl := template .Must (template .New ("sql" ).Parse (s ))
353
391
var buf bytes.Buffer
354
- tmpl .Execute (& buf , nuclidefilter (* nucs ))
355
- fmt .Println (buf .String ())
392
+ tmpl .Execute (& buf , filter )
356
393
customSql [cmd ] = buf .String ()
357
394
buff := doCustom (cmd , simid , proto , simid )
358
395
if * plotit {
0 commit comments