Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/supervisor/cmd/top.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"encoding/json"
10
"fmt"
11
"os"
12
"text/tabwriter"
13
"time"
14
15
"github.com/spf13/cobra"
16
17
"github.com/gitpod-io/gitpod/common-go/log"
18
"github.com/gitpod-io/gitpod/supervisor/api"
19
"github.com/gitpod-io/gitpod/supervisor/pkg/supervisor"
20
)
21
22
var topOpts struct {
23
Json bool
24
Standalone bool
25
}
26
27
var topCmd = &cobra.Command{
28
Use: "top",
29
Short: "Display resource (CPU/memory) usage of the workspace",
30
Run: func(cmd *cobra.Command, args []string) {
31
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
32
defer cancel()
33
34
var (
35
status *api.ResourcesStatusResponse
36
err error
37
)
38
if topOpts.Standalone {
39
status, err = supervisor.Top(ctx)
40
} else {
41
client := api.NewStatusServiceClient(dialSupervisor())
42
status, err = client.ResourcesStatus(ctx, &api.ResourcesStatuRequest{})
43
}
44
if err != nil {
45
log.WithError(err).Fatal("failed to resolve")
46
}
47
48
if topOpts.Json {
49
content, _ := json.Marshal(status)
50
fmt.Println(string(content))
51
} else {
52
tw := tabwriter.NewWriter(os.Stdout, 6, 4, 3, ' ', 0)
53
defer tw.Flush()
54
55
fmt.Fprintf(tw, "CPU(millicores)\tMEMORY(bytes)\n")
56
57
cpuFraction := int64((float64(status.Cpu.Used) / float64(status.Cpu.Limit)) * 100)
58
memFraction := int64((float64(status.Memory.Used) / float64(status.Memory.Limit)) * 100)
59
60
fmt.Fprintf(tw, "%dm/%dm (%d%%)\t%dMi/%dMi (%d%%)\n", status.Cpu.Used, status.Cpu.Limit, cpuFraction, status.Memory.Used/(1024*1024), status.Memory.Limit/(1024*1024), memFraction)
61
}
62
},
63
}
64
65
func init() {
66
rootCmd.AddCommand(topCmd)
67
topCmd.Flags().BoolVarP(&topOpts.Json, "json", "j", false, "print like json")
68
topCmd.Flags().BoolVarP(&topOpts.Standalone, "standalone", "s", false, "standalone mode")
69
}
70
71