├── .gitignore ├── LICENSE ├── README.md ├── go.mod ├── go.sum ├── main.go └── util.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.dll 4 | *.so 5 | *.dylib 6 | golop 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Golop 2 | 3 | A pure Go re-implementation of genlop 4 | 5 | ## Description 6 | 7 | On Gentoo systems, [genlop](https://wiki.gentoo.org/wiki/Project:Perl) is the 8 | tool of choice to get information about current and past compilations, 9 | including the history of packages merged, the median time a compile takes and 10 | an estimate when any currently running compilations will finish. The tool is 11 | written in Perl and all of its output is hard-coded. Since it was originally 12 | written at a time when people would rarely have more than two or three 13 | parallel compiles running, it is rather generous with screen real estate. For 14 | example, this is the output of one package compiling: 15 | 16 | ``` 17 | $ genlop -cn 18 | 19 | Currently merging 1 out of 1 20 | 21 | * app-shells/bash-4.4_p12 22 | 23 | current merge time: 3 seconds. 24 | ETA: less than a minute. 25 | ``` 26 | 27 | On a modern, powerful system, one can easily have 5-10 parallel compilations 28 | happening, which means that it becomes difficult to see what is going on. In 29 | contrast, golop tries to be frugal with screen space: 30 | 31 | ``` 32 | $ golop -c 33 | package elapsed ETA 34 | app-shells/bash-4.4_p12 0s 50s 35 | ``` 36 | 37 | Furthermore, genlop does not have stable sorting for its output, which again 38 | makes it harder to see what is going on with several compiles at the same 39 | time. Golop sorts its output in a stable manner. 40 | 41 | Finally, on slower systems, or when `emerge.log` is long, genlop can take 42 | several seconds to parse all history. Golop is much faster. 43 | 44 | ## Limitations 45 | 46 | There is one major downside to Golop: it is only as portable as Go, which has 47 | been ported to a lot fewer systems than Perl has. One slight upside of using 48 | Go is that it the binary can simply be copied to machines of a compatible 49 | architecture (and libc), since it is statically linked. 50 | 51 | ## Known Bugs and Limitations 52 | 53 | Golop tries to figure out the list of currently running compiles by looking 54 | at the argv of running processes (via the `proc` filesystem). In places where 55 | this filesystem is not accessible, it will break. Golop also relies on the 56 | format of the command line mangling the Portage sandbox does (package names 57 | enclosed in `[]` and `sandbox` being part of `argv[0]`), so it will also break 58 | if Portage ever changes that format. 59 | 60 | There are currently no tests. 61 | 62 | ## Building 63 | 64 | ``` 65 | $ go build . 66 | ``` 67 | 68 | ## Usage 69 | 70 | Golop is not a 100% drop-in for genlop. It has no color output and does not 71 | support all of the information retrieval options genlop has. It still supports 72 | the basic modes of operation genlop has. 73 | 74 | ``` 75 | $ golop -help 76 | Usage of golop: 77 | -c Show current compiles 78 | -e Show history (default true) 79 | -l string 80 | Location of emerge log to parse. (default "/var/log/emerge.log") 81 | -t string 82 | Show history of specific package 83 | ``` 84 | 85 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/klausman/golop 2 | 3 | go 1.15 4 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/klausman/golop/e24d6948cbfa0718d7c62cccb89f4dd989bf588f/go.sum -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Tobias Klausmann 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "flag" 19 | "fmt" 20 | "os" 21 | "time" 22 | ) 23 | 24 | const ( 25 | golopVersion = "0.2.1" 26 | ) 27 | 28 | var ( 29 | modeCurrent = flag.Bool("c", false, "Show current compiles") 30 | modeEstimate = flag.String("t", "", "Show history of specific package") 31 | modeHistory = flag.Bool("e", true, "Show history") 32 | modeVersion = flag.Bool("v", false, "Show golop version information and exit") 33 | logfilename = flag.String("l", "/var/log/emerge.log", "Location of emerge log to parse.") 34 | ) 35 | 36 | func main() { 37 | flag.Parse() 38 | 39 | logfile, err := os.Open(*logfilename) 40 | if err != nil { 41 | fmt.Fprintf(os.Stderr, "Could not open log file '%s': %s\n", *logfilename, err) 42 | os.Exit(1) 43 | } 44 | 45 | if *modeVersion { 46 | fmt.Printf("golop version %s\n", golopVersion) 47 | os.Exit(0) 48 | } 49 | 50 | if *modeCurrent { 51 | mdus := findMedDurations(logfile) 52 | curr, err := runningCompiles() 53 | if err != nil { 54 | panic(err) 55 | } 56 | if len(curr) == 0 { 57 | fmt.Printf("No compilations currently running.\n") 58 | os.Exit(0) 59 | } 60 | 61 | var sts []compileStatus 62 | longest := 0 63 | for _, c := range curr { 64 | status := compileStatus{pkgname: c.pkg, phase: c.phase} 65 | if len(c.pkg) > longest { 66 | longest = len(c.pkg) 67 | } 68 | n, _ := splitpkgver(status.pkgname) 69 | md, ok := mdus[n] 70 | if ok { 71 | eta := md - time.Since(c.start).Round(time.Second) 72 | if eta < 0 { 73 | status.eta = "any time now" 74 | } else { 75 | status.eta = eta.String() 76 | } 77 | } 78 | status.elapsed = time.Since(c.start).Round(time.Second).String() 79 | sts = append(sts, status) 80 | } 81 | fmt.Println(tabulate(sts, longest)) 82 | os.Exit(0) 83 | } 84 | 85 | if *modeEstimate != "" { 86 | compiles, _, durations := findCompileHist(logfile, nil) 87 | var filtered []compileHist 88 | pattern := *modeEstimate 89 | 90 | // We only want the first match of a possible substring, so we replace 91 | // pattern with the full pkgname on first match and then only compare 92 | // literally 93 | firstmatched := false 94 | for _, compile := range compiles { 95 | if firstmatched && compile.pkgname == pattern { 96 | filtered = append(filtered, compile) 97 | continue 98 | } 99 | // We don't have a match yet, compare more generously 100 | if !firstmatched && pkgnameMatch(compile.pkgname, pattern) { 101 | filtered = append(filtered, compile) 102 | pattern = compile.pkgname 103 | firstmatched = true 104 | } 105 | } 106 | if len(filtered) == 0 { 107 | fmt.Printf("Found no compilations matching %s\n", *modeEstimate) 108 | os.Exit(2) 109 | } 110 | 111 | hists := showHistory(filtered, time.Unix(0, 0)) 112 | // Since we have turned the pattern into the full pkgname, this will 113 | // always succeed 114 | durs := durations[pattern] 115 | meddur := medDuration(durs) 116 | 117 | fmt.Printf("%s\nMedian duration: %+v\n", hists, meddur.Round(time.Second)) 118 | os.Exit(0) 119 | } 120 | 121 | if *modeHistory { 122 | compiles, _, _ := findCompileHist(logfile, nil) 123 | fmt.Printf("%s\n", showHistory(compiles, time.Unix(0, 0))) 124 | os.Exit(0) 125 | } 126 | 127 | // If we reach this spot, the user deactivated modeHistory explicitly and 128 | // did not activate another mode 129 | flag.Usage() 130 | os.Exit(1) 131 | 132 | } 133 | 134 | type compileHist struct { 135 | start time.Time 136 | end time.Time 137 | dur time.Duration 138 | pkgname string 139 | pkgversion string 140 | } 141 | 142 | type compileStatus struct { 143 | pkgname string 144 | elapsed string 145 | eta string 146 | phase string 147 | } 148 | -------------------------------------------------------------------------------- /util.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Tobias Klausmann 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "bufio" 19 | "flag" 20 | "fmt" 21 | "io/ioutil" 22 | "os" 23 | "path" 24 | "regexp" 25 | "sort" 26 | "strconv" 27 | "strings" 28 | "time" 29 | ) 30 | 31 | var ( 32 | compileStartRegEx *regexp.Regexp 33 | compileCompleteRegEx *regexp.Regexp 34 | unmergeStartRegEx *regexp.Regexp 35 | splitpkgverRegEx *regexp.Regexp 36 | latestStart map[string]int64 37 | 38 | procDir = flag.String("d", "/proc", "Root of /proc filesystem") 39 | ) 40 | 41 | func init() { 42 | commonRegEx := `\((?P\d+) of (?P\d+)\) (?P[A-Za-z0-9/_-]+)-(?P\d[^ ]+) to /` 43 | compileStartRegEx = regexp.MustCompile(`>>> emerge ` + commonRegEx) 44 | compileCompleteRegEx = regexp.MustCompile(`::: completed emerge ` + commonRegEx) 45 | unmergeStartRegEx = regexp.MustCompile(`=== Unmerging... \((?P[A-Za-z0-9\/_-]+)-(?P\d.*)\)`) 46 | splitpkgverRegEx = regexp.MustCompile(`(?P[A-Za-z0-9/_-]+)-(?P\d[^ ]+)`) 47 | latestStart = make(map[string]int64) 48 | } 49 | 50 | func showHistory(compiles []compileHist, start time.Time) string { 51 | var ret []string 52 | var shown int 53 | for _, compile := range compiles { 54 | if compile.start.UnixNano() >= start.UnixNano() { 55 | shown++ 56 | ret = append(ret, 57 | fmt.Sprintf("%s: %s-%s: %+v", 58 | compile.start.Format(time.RFC3339), compile.pkgname, compile.pkgversion, 59 | compile.dur.Round(time.Second))) 60 | } 61 | } 62 | ret = append(ret, 63 | fmt.Sprintf("Total number of compilations: %d", shown)) 64 | return strings.Join(ret, "\n") 65 | } 66 | 67 | func getReMatches(re *regexp.Regexp, tomatch string) map[string]string { 68 | m := re.FindStringSubmatch(tomatch) 69 | ret := make(map[string]string) 70 | for i, name := range re.SubexpNames() { 71 | if i != 0 { 72 | ret[name] = m[i] 73 | } 74 | } 75 | return ret 76 | } 77 | 78 | func findMedDurations(fd *os.File) map[string]time.Duration { 79 | var lineno int 80 | 81 | durations := make(map[string][]time.Duration) 82 | inprogress := make(map[string]compileHist) 83 | pkg2md := make(map[string]time.Duration) 84 | 85 | scanner := bufio.NewScanner(fd) 86 | for scanner.Scan() { 87 | line := scanner.Text() 88 | lineno++ 89 | fields := strings.Fields(line) 90 | if len(fields) < 2 { 91 | // A line we can't parse: we'll have to just ignore it 92 | continue 93 | } 94 | message := strings.Join(fields[1:], " ") 95 | ts, err := strconv.ParseInt(fields[0][:len(fields[0])-1], 10, 0) 96 | if err != nil { 97 | fmt.Fprintf(os.Stderr, "Could not parse timestamp on line %d: %+v\n", lineno, err) 98 | continue 99 | } 100 | dt := time.Unix(ts, 0) 101 | if compileStartRegEx.MatchString(message) { 102 | pv, ch := extractEmStart(message, dt) 103 | inprogress[pv] = ch 104 | p, _ := splitpkgver(pv) 105 | latestStart[p] = ts 106 | continue 107 | } 108 | if compileCompleteRegEx.MatchString(message) { 109 | pv := extractComplete(message) 110 | c, ok := inprogress[pv] 111 | if !ok { 112 | continue 113 | } 114 | c.end = dt 115 | c.dur = c.end.Sub(c.start) 116 | durations[c.pkgname] = append(durations[c.pkgname], c.dur) 117 | delete(inprogress, pv) 118 | continue 119 | } 120 | } 121 | for pn, durs := range durations { 122 | pkg2md[pn] = medDuration(durs) 123 | } 124 | return pkg2md 125 | } 126 | 127 | func extractEmStart(message string, dt time.Time) (string, compileHist) { 128 | values := getReMatches(compileStartRegEx, message) 129 | c := compileHist{ 130 | start: dt, 131 | pkgname: values["package"], 132 | pkgversion: values["version"], 133 | } 134 | return fmt.Sprintf("%s-%s", values["package"], values["version"]), c 135 | } 136 | 137 | func extractComplete(message string) string { 138 | values := getReMatches(compileCompleteRegEx, message) 139 | return fmt.Sprintf("%v-%v", values["package"], values["version"]) 140 | } 141 | 142 | func medDuration(durs sortableDurs) time.Duration { 143 | sort.Sort(durs) 144 | if len(durs)%2 != 0 { 145 | // And odd number of elements there is a definite middle element 146 | return durs[len(durs)/2] 147 | } 148 | // An even number of elements means we need to average the midmodst pair 149 | mph := len(durs) / 2 150 | mpl := mph - 1 151 | return durs[(mpl+mph)/2] 152 | } 153 | 154 | type sortableDurs []time.Duration 155 | 156 | func (d sortableDurs) Len() int { return len(d) } 157 | func (d sortableDurs) Less(i, j int) bool { return d[i] < d[j] } 158 | func (d sortableDurs) Swap(i, j int) { d[i], d[j] = d[j], d[i] } 159 | 160 | func pkgnameMatch(pkgname, pattern string) bool { 161 | if pkgname == pattern { 162 | return true 163 | } 164 | components := strings.Split(pkgname, "/") 165 | if len(components) != 2 { 166 | // This should never happen, but let's be defensive 167 | return false 168 | } 169 | if components[1] == pattern { 170 | return true 171 | } 172 | return false 173 | } 174 | 175 | func tabulate(p []compileStatus, longest int) string { 176 | var out []string 177 | tmpl := fmt.Sprintf("%%%ds %%10s %%10s %%-s", longest) 178 | out = append(out, fmt.Sprintf(tmpl, "Package", "Phase", "Elapsed", "ETA")) 179 | for _, c := range p { 180 | out = append(out, fmt.Sprintf(tmpl, c.pkgname, c.phase, c.elapsed, c.eta)) 181 | } 182 | return strings.Join(out, "\n") 183 | } 184 | 185 | func runningCompiles() ([]runningCompile, error) { 186 | var currpkgs []runningCompile 187 | pl, err := processes(*procDir) 188 | if err != nil { 189 | return nil, err 190 | } 191 | for _, p := range pl { 192 | if len(p.Cmdline) > 1 && 193 | strings.HasPrefix(p.Cmdline[0], "[") && 194 | strings.HasSuffix(p.Cmdline[0], "sandbox") { 195 | 196 | var s time.Time 197 | pkg := strings.Split(p.Cmdline[0][1:], "]")[0] 198 | tok := strings.Split(p.Cmdline[len(p.Cmdline)-1], " ") 199 | phase := tok[len(tok)-1] 200 | p, _ := splitpkgver(pkg) 201 | if ct, ok := latestStart[p]; ok { 202 | s = time.Unix(ct, 0) 203 | } else { 204 | continue 205 | } 206 | currpkgs = append(currpkgs, runningCompile{pkg: pkg, start: s, phase: phase}) 207 | } 208 | } 209 | return currpkgs, nil 210 | } 211 | 212 | type runningCompile struct { 213 | pkg string 214 | start time.Time 215 | phase string 216 | } 217 | 218 | func splitpkgver(pv string) (string, string) { 219 | values := getReMatches(splitpkgverRegEx, pv) 220 | return values["package"], values["version"] 221 | } 222 | 223 | func findCompileHist(fd *os.File, running map[string]bool) ([]compileHist, map[string]compileHist, map[string][]time.Duration) { 224 | var lineno int 225 | var compiles []compileHist 226 | 227 | durations := make(map[string][]time.Duration) 228 | inprogress := make(map[string]compileHist) 229 | // Unmerge history is not completely implemented yet 230 | unmerges := make(map[string]compileHist) 231 | 232 | scanner := bufio.NewScanner(fd) 233 | for scanner.Scan() { 234 | line := scanner.Text() 235 | lineno++ 236 | fields := strings.Fields(line) 237 | if len(fields) < 2 { 238 | // A line we can't parse: we'll have to just ignore it 239 | continue 240 | } 241 | message := strings.Join(fields[1:], " ") 242 | ts, err := strconv.ParseInt(fields[0][:len(fields[0])-1], 10, 0) 243 | if err != nil { 244 | fmt.Fprintf(os.Stderr, "Could not parse timestamp on line %d: %+v\n", lineno, err) 245 | } 246 | dt := time.Unix(ts, 0) 247 | if compileStartRegEx.MatchString(message) { 248 | values := getReMatches(compileStartRegEx, message) 249 | c := compileHist{ 250 | start: dt, 251 | pkgname: values["package"], 252 | pkgversion: values["version"], 253 | } 254 | inprogress[fmt.Sprintf("%v-%v", values["package"], values["version"])] = c 255 | continue 256 | } 257 | if unmergeStartRegEx.MatchString(message) { 258 | values := getReMatches(unmergeStartRegEx, message) 259 | c := compileHist{ 260 | start: dt, 261 | pkgname: values["package"], 262 | pkgversion: values["version"], 263 | } 264 | unmerges[fmt.Sprintf("%v-%v", values["package"], values["version"])] = c 265 | continue 266 | } 267 | if compileCompleteRegEx.MatchString(message) { 268 | values := getReMatches(compileCompleteRegEx, message) 269 | pkgver := fmt.Sprintf("%v-%v", values["package"], values["version"]) 270 | c, ok := inprogress[pkgver] 271 | if !ok { 272 | if c, ok = unmerges[pkgver]; ok { 273 | delete(unmerges, pkgver) 274 | } 275 | continue 276 | } 277 | c.end = dt 278 | c.dur = c.end.Sub(c.start) 279 | compiles = append(compiles, c) 280 | durations[c.pkgname] = append(durations[c.pkgname], c.dur) 281 | delete(inprogress, pkgver) 282 | continue 283 | } 284 | } 285 | nip := make(map[string]compileHist) 286 | for k, v := range inprogress { 287 | if running[k] { 288 | nip[k] = v 289 | } 290 | } 291 | return compiles, nip, durations 292 | } 293 | 294 | func processes(dirname string) ([]process, error) { 295 | var ps []process 296 | entries, err := ioutil.ReadDir(dirname) 297 | if err != nil { 298 | return nil, err 299 | } 300 | for _, entry := range entries { 301 | fp := path.Join(dirname, entry.Name()) 302 | fi, err := os.Stat(fp) 303 | if err != nil { 304 | continue 305 | } 306 | if fi.IsDir() { 307 | var p process 308 | p.PID, err = strconv.ParseUint(entry.Name(), 10, 64) 309 | if err == nil { 310 | data, err := ioutil.ReadFile(path.Join(fp, "cmdline")) 311 | if err != nil { 312 | continue 313 | } 314 | p.Cmdline = dropEmptyStr(strings.Split(string(data), "\000")) 315 | ps = append(ps, p) 316 | } 317 | } 318 | 319 | } 320 | return ps, nil 321 | } 322 | 323 | type process struct { 324 | PID uint64 325 | Cmdline []string 326 | } 327 | 328 | func dropEmptyStr(ss []string) []string { 329 | var r []string 330 | for _, s := range ss { 331 | if s != "" { 332 | r = append(r, s) 333 | } 334 | } 335 | return r 336 | } 337 | --------------------------------------------------------------------------------