Skip to content

Commit aa1e196

Browse files
committed
counter: support 386 systems
Modify the way mmap is invoked on Windows systems to support our technique for extending the size of counter files. The existing code was overelaborate and failed on 386. Also, stop generating empty file names for uploading. Fixes golang/go#65447 Fixes golang/go#60692 Fixes golang/go#60615 Change-Id: I8349d250fb516c188850557d3d099378248fb17b Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/561995 Run-TryBot: Peter Weinberger <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 7b892fc commit aa1e196

File tree

6 files changed

+69
-56
lines changed

6 files changed

+69
-56
lines changed

counter/counter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build go1.19 && !openbsd && !js && !wasip1 && !solaris && !android && !plan9 && !386
5+
//go:build go1.19 && !openbsd && !js && !wasip1 && !solaris && !android
66

77
package counter
88

counter/counter_disabled.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build !go1.19 || openbsd || js || wasip1 || solaris || android || plan9 || 386
5+
//go:build !go1.19 || openbsd || js || wasip1 || solaris || android
66

77
package counter
88

counter/countertest/countertest_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build go1.21
5+
//go:build go1.21 && !openbsd && !js && !wasip1 && !solaris && !android
66

77
package countertest
88

internal/mmap/mmap_windows.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,20 @@ func mmapFile(f *os.File, previous *Data) (Data, error) {
2525
if size == 0 {
2626
return Data{f, nil, nil}, nil
2727
}
28+
// setting the min and max sizes to zero to map the whole file, as described in
29+
// https://learn.microsoft.com/en-us/windows/win32/memory/creating-a-file-mapping-object#file-mapping-size
2830
h, err := windows.CreateFileMapping(windows.Handle(f.Fd()), nil, syscall.PAGE_READWRITE, 0, 0, nil)
2931
if err != nil {
3032
return Data{}, fmt.Errorf("CreateFileMapping %s: %w", f.Name(), err)
3133
}
32-
34+
// the mapping extends from zero to the end of the file mapping
35+
// https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile
3336
addr, err := windows.MapViewOfFile(h, syscall.FILE_MAP_READ|syscall.FILE_MAP_WRITE, 0, 0, 0)
3437
if err != nil {
3538
return Data{}, fmt.Errorf("MapViewOfFile %s: %w", f.Name(), err)
3639
}
37-
var info windows.MemoryBasicInformation
38-
err = windows.VirtualQuery(addr, &info, unsafe.Sizeof(info))
39-
if err != nil {
40-
return Data{}, fmt.Errorf("VirtualQuery %s: %w", f.Name(), err)
41-
}
42-
data := unsafe.Slice((*byte)(unsafe.Pointer(addr)), int(info.RegionSize))
43-
return Data{f, data, h}, nil
40+
// need to remember addr and h for unmapping
41+
return Data{f, unsafe.Slice((*byte)(unsafe.Pointer(addr)), size), h}, nil
4442
}
4543

4644
func munmapFile(d Data) error {

internal/regtest/e2e_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
)
2525

2626
func TestRunProg(t *testing.T) {
27+
testenv.SkipIfUnsupportedPlatform(t)
2728
testenv.MustHaveExec(t)
2829
prog1 := NewProgram(t, "prog1", func() int {
2930
fmt.Println("FuncB")
@@ -55,6 +56,7 @@ func programIncCounters() int {
5556
}
5657

5758
func TestE2E_off(t *testing.T) {
59+
testenv.SkipIfUnsupportedPlatform(t)
5860
testenv.MustHaveExec(t)
5961
testenv.SkipIfUnsupportedPlatform(t)
6062

@@ -94,6 +96,7 @@ func TestE2E_off(t *testing.T) {
9496
}
9597

9698
func TestE2E(t *testing.T) {
99+
testenv.SkipIfUnsupportedPlatform(t)
97100
testenv.MustHaveExec(t)
98101
testenv.SkipIfUnsupportedPlatform(t)
99102
programIncCounters := NewProgram(t, "prog", programIncCounters)

internal/upload/reports.go

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ func (u *Uploader) reports(todo *work) ([]string, error) {
5757
if err != nil {
5858
return nil, err
5959
}
60-
todo.readyfiles = append(todo.readyfiles, fname)
60+
if fname != "" {
61+
todo.readyfiles = append(todo.readyfiles, fname)
62+
}
6163
}
6264
return todo.readyfiles, nil
6365
}
@@ -176,81 +178,91 @@ func (u *Uploader) createReport(start time.Time, expiryDate string, files []stri
176178
if err := json.Unmarshal(localContents, &x); err != nil {
177179
return "", fmt.Errorf("failed to unmarshal local report (%v)", err)
178180
}
179-
// 2. create the uploadable version
180-
cfg := config.NewConfig(u.Config)
181-
upload := &telemetry.Report{
182-
Week: report.Week,
183-
LastWeek: report.LastWeek,
184-
X: report.X,
185-
Config: report.Config,
186-
}
187-
for _, p := range report.Programs {
188-
// does the uploadConfig want this program?
189-
// if so, copy over the Stacks and Counters
190-
// that the uploadConfig mentions.
191-
if !cfg.HasGoVersion(p.GoVersion) || !cfg.HasProgram(p.Program) || !cfg.HasVersion(p.Program, p.Version) {
192-
continue
193-
}
194-
x := &telemetry.ProgramReport{
195-
Program: p.Program,
196-
Version: p.Version,
197-
GOOS: p.GOOS,
198-
GOARCH: p.GOARCH,
199-
GoVersion: p.GoVersion,
200-
Counters: make(map[string]int64),
201-
Stacks: make(map[string]int64),
181+
182+
var uploadContents []byte
183+
if uploadOK {
184+
// 2. create the uploadable version
185+
cfg := config.NewConfig(u.Config)
186+
upload := &telemetry.Report{
187+
Week: report.Week,
188+
LastWeek: report.LastWeek,
189+
X: report.X,
190+
Config: report.Config,
202191
}
203-
upload.Programs = append(upload.Programs, x)
204-
for k, v := range p.Counters {
205-
if cfg.HasCounter(p.Program, k) && report.X <= cfg.Rate(p.Program, k) {
206-
x.Counters[k] = v
192+
for _, p := range report.Programs {
193+
// does the uploadConfig want this program?
194+
// if so, copy over the Stacks and Counters
195+
// that the uploadConfig mentions.
196+
if !cfg.HasGoVersion(p.GoVersion) || !cfg.HasProgram(p.Program) || !cfg.HasVersion(p.Program, p.Version) {
197+
continue
207198
}
208-
}
209-
// and the same for Stacks
210-
// this can be made more efficient, when it matters
211-
for k, v := range p.Stacks {
212-
before, _, _ := strings.Cut(k, "\n")
213-
if cfg.HasStack(p.Program, before) && report.X <= cfg.Rate(p.Program, before) {
214-
x.Stacks[k] = v
199+
x := &telemetry.ProgramReport{
200+
Program: p.Program,
201+
Version: p.Version,
202+
GOOS: p.GOOS,
203+
GOARCH: p.GOARCH,
204+
GoVersion: p.GoVersion,
205+
Counters: make(map[string]int64),
206+
Stacks: make(map[string]int64),
207+
}
208+
upload.Programs = append(upload.Programs, x)
209+
for k, v := range p.Counters {
210+
if cfg.HasCounter(p.Program, k) && report.X <= cfg.Rate(p.Program, k) {
211+
x.Counters[k] = v
212+
}
213+
}
214+
// and the same for Stacks
215+
// this can be made more efficient, when it matters
216+
for k, v := range p.Stacks {
217+
before, _, _ := strings.Cut(k, "\n")
218+
if cfg.HasStack(p.Program, before) && report.X <= cfg.Rate(p.Program, before) {
219+
x.Stacks[k] = v
220+
}
215221
}
216222
}
217-
}
218223

219-
uploadContents, err := json.MarshalIndent(upload, "", " ")
220-
if err != nil {
221-
return "", fmt.Errorf("failed to marshal upload report (%v)", err)
224+
uploadContents, err = json.MarshalIndent(upload, "", " ")
225+
if err != nil {
226+
return "", fmt.Errorf("failed to marshal upload report (%v)", err)
227+
}
222228
}
223229
localFileName := filepath.Join(u.LocalDir, "local."+expiryDate+".json")
224230
uploadFileName := filepath.Join(u.LocalDir, expiryDate+".json")
231+
232+
/* Prepare to write files */
225233
// if either file exists, someone has been here ahead of us
226234
// (there is still a race, but this check shortens the open window)
227235
if _, err := os.Stat(localFileName); err == nil {
228236
deleteFiles(files)
229-
return "", fmt.Errorf("report %s already exists", localFileName)
237+
return "", fmt.Errorf("local report %s already exists", localFileName)
230238
}
231239
if _, err := os.Stat(uploadFileName); err == nil {
232240
deleteFiles(files)
233-
return "", fmt.Errorf("local report %s already exists", uploadFileName)
241+
return "", fmt.Errorf("report %s already exists", uploadFileName)
234242
}
235243
// write the uploadable file
236244
var errUpload, errLocal error
237245
if uploadOK {
238246
errUpload = os.WriteFile(uploadFileName, uploadContents, 0644)
239247
}
240-
// save all local reports.
248+
// write the local file
241249
errLocal = os.WriteFile(localFileName, localContents, 0644)
250+
/* Wrote the files */
242251

243252
// even though these errors won't occur, what should happen
244253
// if errUpload == nil and it is ok to upload, and errLocal != nil?
245254
if errLocal != nil {
246-
return "", fmt.Errorf("failed to write local file %s (%v)", uploadFileName, errLocal)
255+
return "", fmt.Errorf("failed to write local file %s (%v)", localFileName, errLocal)
247256
}
248257
if errUpload != nil {
249258
return "", fmt.Errorf("failed to write upload file %s (%v)", uploadFileName, errUpload)
250259
}
251-
logger.Printf("created %s, deleting %v", uploadFileName, files)
260+
logger.Printf("created %q, deleting %v", uploadFileName, files)
252261
deleteFiles(files)
253-
return uploadFileName, nil
262+
if uploadOK {
263+
return uploadFileName, nil
264+
}
265+
return "", nil
254266
}
255267

256268
// return an existing ProgremReport, or create anew

0 commit comments

Comments
 (0)