Skip to content
This repository has been archived by the owner on Feb 17, 2024. It is now read-only.

timecraft: add profile resource #33

Merged
merged 7 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions format/logsegment/logsegment.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,6 @@ table Record {
function_call:[ubyte];
}

// Details about a function call.
table FunctionCall {
// State of the WebAssembly stack before and after the function was called.
// The first {param_count} values are the input parameters, and the remaining
// values are the return values.
stack:[ulong];
// Captured sections of the WebAssembly module's linear memory, stored
// contiguously and indexed by {memory_access}.
memory:[ubyte];
// Ordered collection of memory reads and writes made by the function.
memory_access:[MemoryAccess];
}

// MemoryAccess represents the capture of a section of memory.
struct MemoryAccess {
// Byte offset in the WebAssembly module's linear memory where the memory
// access starts.
offset:uint;
// Byte offset into {FunctionCall.memory}. The length of the captured memory
// can be derived by comparing the {index_offset} with that of the next
// {FunctionCall.memory_access}. The length of the final memory region can
// be derived by comparing the {index_offset} with the length of
// {FunctionCall.memory}.
index_offset:uint;
}

Comment on lines -45 to -70
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to the actual change but was unused.

root_type RecordBatch;

file_identifier "TL.0";
Expand Down
165 changes: 0 additions & 165 deletions format/logsegment/logsegment_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions format/timecraft.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const (
TypeTimecraftRuntime MediaType = "application/vnd.timecraft.runtime.v1+json"
TypeTimecraftConfig MediaType = "application/vnd.timecraft.config.v1+json"
TypeTimecraftProcess MediaType = "application/vnd.timecraft.process.v1+json"
TypeTimecraftProfile MediaType = "application/vnd.timecraft.profile.v1+pprof"
TypeTimecraftManifest MediaType = "application/vnd.timecraft.manifest.v1+json"
TypeTimecraftModule MediaType = "application/vnd.timecraft.module.v1+wasm"
)
Expand Down
109 changes: 85 additions & 24 deletions internal/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"os"
"strconv"
"strings"
"text/tabwriter"
"time"

pprof "github.com/google/pprof/profile"
"github.com/google/uuid"
"github.com/stealthrocket/timecraft/format"
"github.com/stealthrocket/timecraft/internal/print/human"
Expand Down Expand Up @@ -56,29 +58,13 @@ func describe(ctx context.Context, args []string) error {
flagSet := newFlagSet("timecraft describe", describeUsage)
customVar(flagSet, &output, "o", "output")
customVar(flagSet, &registryPath, "r", "registry")
parseFlags(flagSet, args)
args = parseFlags(flagSet, args)

args = flagSet.Args()
if len(args) == 0 {
return errors.New(`expected one resource id as argument`)
return errors.New(`expected a resource type as argument`)
}
resourceTypeLookup := args[0]
resourceIDs := []string{}
args = args[1:]

for len(args) > 0 {
parseFlags(flagSet, args)
args = flagSet.Args()

i := slices.IndexFunc(args, func(s string) bool {
return strings.HasPrefix(s, "-")
})
if i < 0 {
i = len(args)
}
resourceIDs = append(resourceIDs, args[:i]...)
args = args[i:]
}
resourceIDs := args[1:]

resource, ok := findResource(resourceTypeLookup, resources[:])
if !ok {
Expand Down Expand Up @@ -172,7 +158,7 @@ func describeConfig(ctx context.Context, reg *timemachine.Registry, id string) (
version = r.Version
}
desc := &configDescriptor{
id: d.Digest.Short(),
id: d.Digest.String(),
runtime: runtimeDescriptor{
runtime: runtime,
version: version,
Expand All @@ -183,7 +169,7 @@ func describeConfig(ctx context.Context, reg *timemachine.Registry, id string) (
}
for i, module := range c.Modules {
desc.modules[i] = moduleDescriptor{
id: module.Digest.Short(),
id: module.Digest.String(),
name: moduleName(module),
size: human.Bytes(module.Size),
}
Expand All @@ -201,7 +187,7 @@ func describeModule(ctx context.Context, reg *timemachine.Registry, id string) (
return nil, err
}
desc := &moduleDescriptor{
id: d.Digest.Short(),
id: d.Digest.String(),
name: moduleName(d),
size: human.Bytes(len(m.Code)),
}
Expand Down Expand Up @@ -243,7 +229,7 @@ func describeProcess(ctx context.Context, reg *timemachine.Registry, id string)

for i, module := range c.Modules {
desc.modules[i] = moduleDescriptor{
id: module.Digest.Short(),
id: module.Digest.String(),
name: moduleName(module),
size: human.Bytes(module.Size),
}
Expand All @@ -268,6 +254,24 @@ func describeProcess(ctx context.Context, reg *timemachine.Registry, id string)
return desc, nil
}

func describeProfile(ctx context.Context, reg *timemachine.Registry, id string) (any, error) {
d, err := reg.LookupDescriptor(ctx, format.ParseHash(id))
if err != nil {
return nil, err
}
p, err := reg.LookupProfile(ctx, d.Digest)
if err != nil {
return nil, err
}
desc := &profileDescriptor{
id: d.Digest.String(),
processID: d.Annotations["timecraft.process.id"],
profileType: d.Annotations["timecraft.profile.type"],
profile: p,
}
return desc, nil
}

func describeRuntime(ctx context.Context, reg *timemachine.Registry, id string) (any, error) {
d, err := reg.LookupDescriptor(ctx, format.ParseHash(id))
if err != nil {
Expand All @@ -278,7 +282,7 @@ func describeRuntime(ctx context.Context, reg *timemachine.Registry, id string)
return nil, err
}
desc := &runtimeDescriptor{
id: d.Digest.Short(),
id: d.Digest.String(),
runtime: r.Runtime,
version: r.Version,
}
Expand All @@ -301,6 +305,10 @@ func lookupProcess(ctx context.Context, reg *timemachine.Registry, id string) (a
return descriptorAndData(desc, proc), nil
}

func lookupProfile(ctx context.Context, reg *timemachine.Registry, id string) (any, error) {
return lookup(ctx, reg, id, (*timemachine.Registry).LookupProfile)
}

func lookupRuntime(ctx context.Context, reg *timemachine.Registry, id string) (any, error) {
return lookup(ctx, reg, id, (*timemachine.Registry).LookupRuntime)
}
Expand Down Expand Up @@ -412,6 +420,59 @@ func (desc *processDescriptor) Format(w fmt.State, _ rune) {
}
}

type profileDescriptor struct {
id string
processID string
profileType string
profile *pprof.Profile
}

func (desc *profileDescriptor) Format(w fmt.State, _ rune) {
startTime := human.Time(time.Unix(0, desc.profile.TimeNanos))
duration := human.Duration(desc.profile.DurationNanos)

fmt.Fprintf(w, "ID: %s\n", desc.id)
fmt.Fprintf(w, "Type: %s\n", desc.profileType)
fmt.Fprintf(w, "Process: %s\n", desc.processID)
fmt.Fprintf(w, "Start: %s, %s\n", startTime, time.Time(startTime).Format(time.RFC1123))
fmt.Fprintf(w, "Duration: %s\n", duration)

if period := desc.profile.Period; period != 0 {
fmt.Fprintf(w, "Period: %d %s/%s\n", period, desc.profile.PeriodType.Type, desc.profile.PeriodType.Unit)
} else {
fmt.Fprintf(w, "Period: (none)\n")
}

fmt.Fprintf(w, "Samples: %d\n", len(desc.profile.Sample))
hasDefault := false
tw := tabwriter.NewWriter(w, 0, 4, 2, ' ', 0)
for i, sampleType := range desc.profile.SampleType {
if i != 0 {
fmt.Fprintf(tw, "\n")
}
fmt.Fprintf(w, "- %s\t%s", sampleType.Type, sampleType.Unit)
if sampleType.Type == desc.profile.DefaultSampleType {
hasDefault = true
fmt.Fprintf(tw, " (default)")
}
}
if !hasDefault {
fmt.Fprintf(tw, " (default)\n")
} else {
fmt.Fprintf(tw, "\n")
}
_ = tw.Flush()

if comments := desc.profile.Comments; len(comments) == 0 {
fmt.Fprintf(w, "Comments: (none)\n")
} else {
fmt.Fprintf(w, "Comments:\n")
for _, comment := range comments {
fmt.Fprintf(w, "%s\n", comment)
}
}
}

type runtimeDescriptor struct {
id string
runtime string
Expand Down
Loading
Loading