CodeSampleX

샘플

github.com/prometheus/procfs v0.22.0: FS.VM

검증된 샘플 — golang github.com/prometheus/procfs v0.22.0: FS.VM. go 1.26 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다: procfs.FS.VM reads and parses…

sha256:0aa8b682d179fc23010a597fc9206fa20ba7ebb44466c167d0114ea3c77bd660

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
1
빌드한 서명 키
1
선언된 환경 linux 24 · ubuntu · glibc 2.39 x64 go

검증 실행 환경

환경 컨트랙트 단계 실행일
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-05

케이스

HOW
목표
verify procfs.FS.VM in pkg:golang/github.com/prometheus/procfs@v0.22.0
패키지
심벌
  • procfs.FS.VM
생성일
2026-09-05T08:45:30Z

컨트랙트

  1. procfs.FS.VM reads and parses numerical sysctl virtual memory parameters from sys/vm
  2. procfs.FS.VM parses lowmem_reserve_ratio as an array of numerical values
  3. procfs.FS.VM parses numa_zonelist_order as a string
  4. procfs.FS.VM returns an error when sys/vm does not exist or is not a directory
  5. procfs.FS.VM returns an error when sys/vm parameter value cannot be parsed as an integer

파일

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

소스 아티팩트 내려받기 (tar.gz)

소스

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify procfs.FS.VM in pkg:golang/github.com/prometheus/procfs@v0.22.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/prometheus/procfs@v0.22.0
Demonstrate these symbols/APIs:
  - procfs.FS.VM

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:b01d8a2d626c3976d9f639dbd133092c390c77d04ce2d0207ce7ff92aa9dd0e2","contract":["procfs.FS.VM reads and parses numerical sysctl virtual memory parameters from sys/vm","procfs.FS.VM parses lowmem_reserve_ratio as an array of numerical values","procfs.FS.VM parses numa_zonelist_order as a string","procfs.FS.VM returns an error when sys/vm does not exist or is not a directory","procfs.FS.VM returns an error when sys/vm parameter value cannot be parsed as an integer"],"goal":"verify procfs.FS.VM in pkg:golang/github.com/prometheus/procfs@v0.22.0","kind":"HOW","packages":["pkg:golang/github.com/prometheus/procfs@v0.22.0"],"schemaVersion":1,"symbols":["procfs.FS.VM"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/prometheus/procfs@v0.22.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/prometheus/procfs@v0.22.0","symbols":["procfs.FS.VM"],"verifierAdapter":"golang@1"}
go.mod
module example.com/procfs-sample

go 1.26.6

require github.com/prometheus/procfs v0.22.0

require golang.org/x/sys v0.47.0 // indirect
go.sum
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/prometheus/procfs v0.22.0 h1:6q9+/JL9IKAPbCmBrv9n5O5Ty3NKnciV5X7YGw0oics=
github.com/prometheus/procfs v0.22.0/go.mod h1:CvmFr/GVhIjIvWJZW3tgkODBQMRIf0EyWMQLHCHab58=
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
main.go
package main

import (
	"fmt"
	"os"

	"github.com/prometheus/procfs"
)

// GetVMStats reads virtual memory sysctl tunables from the specified procfs mount point.
func GetVMStats(mountPoint string) (*procfs.VM, error) {
	fs, err := procfs.NewFS(mountPoint)
	if err != nil {
		return nil, fmt.Errorf("failed to open procfs at %q: %w", mountPoint, err)
	}

	vm, err := fs.VM()
	if err != nil {
		return nil, fmt.Errorf("failed to read vm statistics: %w", err)
	}

	return vm, nil
}

func main() {
	vm, err := GetVMStats(procfs.DefaultMountPoint)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Notice: could not read system procfs vm stats: %v\n", err)
		return
	}

	if vm.Swappiness != nil {
		fmt.Printf("swappiness: %d\n", *vm.Swappiness)
	}
	if vm.OvercommitMemory != nil {
		fmt.Printf("overcommit_memory: %d\n", *vm.OvercommitMemory)
	}
	if vm.MaxMapCount != nil {
		fmt.Printf("max_map_count: %d\n", *vm.MaxMapCount)
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify procfs.FS.VM in pkg:golang/github.com/prometheus/procfs@v0.22.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/prometheus/procfs@v0.22.0"
  ],
  "symbols": [
    "procfs.FS.VM"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/prometheus/procfs"
)

func main() {
	// Assertion 1, 2, 3: Create fixture procfs directory with valid sysctl VM entries
	tmpDir, err := os.MkdirTemp("", "procfs-contract-*")
	if err != nil {
		fmt.Fprintf(os.Stderr, "setup failed: %v\n", err)
		os.Exit(1)
	}
	defer os.RemoveAll(tmpDir)

	sysVmDir := filepath.Join(tmpDir, "sys", "vm")
	if err := os.MkdirAll(sysVmDir, 0755); err != nil {
		fmt.Fprintf(os.Stderr, "setup failed to create sys/vm dir: %v\n", err)
		os.Exit(1)
	}

	entries := map[string]string{
		"swappiness":             "60\n",
		"dirty_ratio":            "20\n",
		"dirty_background_ratio": "10\n",
		"overcommit_memory":      "1\n",
		"max_map_count":          "65530\n",
		"drop_caches":            "0\n",
		"page-cluster":           "3\n",
		"vfs_cache_pressure":     "100\n",
		"admin_reserve_kbytes":   "8192\n",
		"stat_interval":          "1\n",
		"lowmem_reserve_ratio":   "256 256 32 0 0\n",
		"numa_zonelist_order":    "Node\n",
	}

	for name, content := range entries {
		filePath := filepath.Join(sysVmDir, name)
		if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
			fmt.Fprintf(os.Stderr, "setup failed to write file %s: %v\n", name, err)
			os.Exit(1)
		}
	}

	fs, err := procfs.NewFS(tmpDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "procfs.NewFS failed: %v\n", err)
		os.Exit(1)
	}

	vm, err := fs.VM()
	if err != nil {
		fmt.Fprintf(os.Stderr, "fs.VM() failed: %v\n", err)
		os.Exit(1)
	}
	if vm == nil {
		fmt.Fprintf(os.Stderr, "fs.VM() returned nil VM struct\n")
		os.Exit(1)
	}

	// 1. procfs.FS.VM reads and parses numerical sysctl virtual memory parameters from sys/vm
	if vm.Swappiness == nil || *vm.Swappiness != 60 {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: swappiness expected 60, got %v\n", vm.Swappiness)
		os.Exit(1)
	}
	if vm.DirtyRatio == nil || *vm.DirtyRatio != 20 {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: dirty_ratio expected 20, got %v\n", vm.DirtyRatio)
		os.Exit(1)
	}
	if vm.DirtyBackgroundRatio == nil || *vm.DirtyBackgroundRatio != 10 {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: dirty_background_ratio expected 10, got %v\n", vm.DirtyBackgroundRatio)
		os.Exit(1)
	}
	if vm.OvercommitMemory == nil || *vm.OvercommitMemory != 1 {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: overcommit_memory expected 1, got %v\n", vm.OvercommitMemory)
		os.Exit(1)
	}
	if vm.MaxMapCount == nil || *vm.MaxMapCount != 65530 {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: max_map_count expected 65530, got %v\n", vm.MaxMapCount)
		os.Exit(1)
	}
	if vm.DropCaches == nil || *vm.DropCaches != 0 {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: drop_caches expected 0, got %v\n", vm.DropCaches)
		os.Exit(1)
	}
	if vm.PageCluster == nil || *vm.PageCluster != 3 {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: page-cluster expected 3, got %v\n", vm.PageCluster)
		os.Exit(1)
	}
	if vm.VfsCachePressure == nil || *vm.VfsCachePressure != 100 {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: vfs_cache_pressure expected 100, got %v\n", vm.VfsCachePressure)
		os.Exit(1)
	}
	if vm.AdminReserveKbytes == nil || *vm.AdminReserveKbytes != 8192 {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: admin_reserve_kbytes expected 8192, got %v\n", vm.AdminReserveKbytes)
		os.Exit(1)
	}
	if vm.StatInterval == nil || *vm.StatInterval != 1 {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: stat_interval expected 1, got %v\n", vm.StatInterval)
		os.Exit(1)
	}

	// 2. procfs.FS.VM parses lowmem_reserve_ratio as an array of numerical values
	if len(vm.LowmemReserveRatio) != 5 {
		fmt.Fprintf(os.Stderr, "assertion 2 failed: lowmem_reserve_ratio expected length 5, got %d\n", len(vm.LowmemReserveRatio))
		os.Exit(1)
	}
	expectedRatios := []int64{256, 256, 32, 0, 0}
	for i, expected := range expectedRatios {
		if vm.LowmemReserveRatio[i] == nil || *vm.LowmemReserveRatio[i] != expected {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: lowmem_reserve_ratio[%d] expected %d, got %v\n", i, expected, vm.LowmemReserveRatio[i])
			os.Exit(1)
		}
	}

	// 3. procfs.FS.VM parses numa_zonelist_order as a string
	if vm.NumaZonelistOrder != "Node" {
		fmt.Fprintf(os.Stderr, "assertion 3 failed: numa_zonelist_order expected 'Node', got %q\n", vm.NumaZonelistOrder)
		os.Exit(1)
	}

	// 4. procfs.FS.VM returns an error when sys/vm does not exist or is not a directory
	emptyDir, err := os.MkdirTemp("", "procfs-empty-*")
	if err != nil {
		fmt.Fprintf(os.Stderr, "setup for empty dir failed: %v\n", err)
		os.Exit(1)
	}
	defer os.RemoveAll(emptyDir)

	emptyFS, err := procfs.NewFS(emptyDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "procfs.NewFS for emptyDir failed: %v\n", err)
		os.Exit(1)
	}
	if _, err := emptyFS.VM(); err == nil {
		fmt.Fprintf(os.Stderr, "assertion 4 failed: expected error when sys/vm does not exist, got nil\n")
		os.Exit(1)
	}

	fileDir, err := os.MkdirTemp("", "procfs-file-*")
	if err != nil {
		fmt.Fprintf(os.Stderr, "setup for file dir failed: %v\n", err)
		os.Exit(1)
	}
	defer os.RemoveAll(fileDir)

	if err := os.MkdirAll(filepath.Join(fileDir, "sys"), 0755); err != nil {
		fmt.Fprintf(os.Stderr, "setup for file dir sys failed: %v\n", err)
		os.Exit(1)
	}
	if err := os.WriteFile(filepath.Join(fileDir, "sys", "vm"), []byte("not a directory"), 0644); err != nil {
		fmt.Fprintf(os.Stderr, "setup for file dir sys/vm file failed: %v\n", err)
		os.Exit(1)
	}
	fileFS, err := procfs.NewFS(fileDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "procfs.NewFS for fileDir failed: %v\n", err)
		os.Exit(1)
	}
	if _, err := fileFS.VM(); err == nil {
		fmt.Fprintf(os.Stderr, "assertion 4 failed: expected error when sys/vm is not a directory, got nil\n")
		os.Exit(1)
	}

	// 5. procfs.FS.VM returns an error when sys/vm parameter value cannot be parsed as an integer
	corruptDir, err := os.MkdirTemp("", "procfs-corrupt-*")
	if err != nil {
		fmt.Fprintf(os.Stderr, "setup for corrupt dir failed: %v\n", err)
		os.Exit(1)
	}
	defer os.RemoveAll(corruptDir)

	corruptSysVm := filepath.Join(corruptDir, "sys", "vm")
	if err := os.MkdirAll(corruptSysVm, 0755); err != nil {
		fmt.Fprintf(os.Stderr, "setup for corrupt sys/vm failed: %v\n", err)
		os.Exit(1)
	}
	if err := os.WriteFile(filepath.Join(corruptSysVm, "swappiness"), []byte("invalid_int\n"), 0644); err != nil {
		fmt.Fprintf(os.Stderr, "setup for corrupt swappiness failed: %v\n", err)
		os.Exit(1)
	}
	corruptFS, err := procfs.NewFS(corruptDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "procfs.NewFS for corruptDir failed: %v\n", err)
		os.Exit(1)
	}
	if _, err := corruptFS.VM(); err == nil {
		fmt.Fprintf(os.Stderr, "assertion 5 failed: expected error when swappiness is not a number, got nil\n")
		os.Exit(1)
	}

	fmt.Println("PASS: all procfs.FS.VM contract assertions passed")
}

오리진 시더

익명