跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

memory.go

81 行 · 2.0 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package qemu
5
6import (
7 "context"
8 "encoding/json"
9 "errors"
10 "fmt"
11 "io"
12 "net"
13 "path/filepath"
14 "time"
15
16 "github.com/docker/go-units"
17 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
18)
19
20func (l *LimaQemuDriver) SetMemoryTarget(ctx context.Context, target uint64) error {
21 if err := ctx.Err(); err != nil {
22 return err
23 }
24 maximum, err := units.RAMInBytes(*l.Instance.Config.Memory)
25 if err != nil {
26 return err
27 }
28 if target < 512<<20 || target > uint64(maximum) || target%(1<<20) != 0 {
29 return errors.New("memory target is outside the VM capacity")
30 }
31 ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
32 defer cancel()
33 conn, err := (&net.Dialer{}).DialContext(ctx, "unix", filepath.Join(l.Instance.Dir, filenames.QMPSock))
34 if err != nil {
35 return err
36 }
37 defer conn.Close()
38 stop := context.AfterFunc(ctx, func() { _ = conn.Close() })
39 defer stop()
40 deadline, _ := ctx.Deadline()
41 if err := conn.SetDeadline(deadline); err != nil {
42 return err
43 }
44 decoder := json.NewDecoder(io.LimitReader(conn, 64<<10))
45 var greeting map[string]json.RawMessage
46 if err := decoder.Decode(&greeting); err != nil {
47 return err
48 }
49 if greeting["QMP"] == nil {
50 return errors.New("invalid QMP greeting")
51 }
52 encoder := json.NewEncoder(conn)
53 for i, command := range []string{"qmp_capabilities", "balloon"} {
54 request := map[string]any{"execute": command, "id": i + 1}
55 if command == "balloon" {
56 request["arguments"] = map[string]uint64{"value": target}
57 }
58 if err := encoder.Encode(request); err != nil {
59 return err
60 }
61 for {
62 var response struct {
63 ID int `json:"id"`
64 Error *struct {
65 Class string `json:"class"`
66 } `json:"error"`
67 }
68 if err := decoder.Decode(&response); err != nil {
69 return err
70 }
71 if response.ID != i+1 {
72 continue
73 }
74 if response.Error != nil {
75 return fmt.Errorf("QMP memory adjustment failed: %s", response.Error.Class)
76 }
77 break
78 }
79 }
80 return nil
81}