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

Commit

Permalink
describe profiles
Browse files Browse the repository at this point in the history
Signed-off-by: Achille Roussel <[email protected]>
  • Loading branch information
achille-roussel committed May 30, 2023
1 parent 1d75325 commit eea6192
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 228 deletions.
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;
}

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.

71 changes: 64 additions & 7 deletions internal/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"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 @@ -156,7 +157,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 @@ -167,7 +168,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 @@ -185,7 +186,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 @@ -227,7 +228,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 @@ -252,8 +253,22 @@ func describeProcess(ctx context.Context, reg *timemachine.Registry, id string)
return desc, nil
}

func describeProfiles(ctx context.Context, reg *timemachine.Registry, id string) (any, error) {
return nil, errors.New("TODO")
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) {
Expand All @@ -266,7 +281,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 @@ -289,6 +304,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 @@ -400,6 +419,44 @@ 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))
for _, sampleType := range desc.profile.SampleType {
fmt.Fprintf(w, "- %s (%s)\n", sampleType.Type, sampleType.Unit)
}

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
9 changes: 7 additions & 2 deletions internal/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ var resources = [...]resource{
describe: describeConfig,
lookup: lookupConfig,
},

{
typ: "log",
alt: []string{"logs"},
mediaType: format.TypeTimecraftManifest,
describe: describeLog,
lookup: describeLog,
},

{
typ: "module",
alt: []string{"mo", "mod", "mods", "modules"},
Expand All @@ -83,6 +85,7 @@ var resources = [...]resource{
describe: describeModule,
lookup: lookupModule,
},

{
typ: "process",
alt: []string{"ps", "proc", "procs", "processes"},
Expand All @@ -91,14 +94,16 @@ var resources = [...]resource{
describe: describeProcess,
lookup: lookupProcess,
},

{
typ: "profile",
alt: []string{"prof", "profs", "profiles"},
mediaType: format.TypeTimecraftProfile,
get: getProfiles,
describe: describeProfiles,
lookup: describeProfiles,
describe: describeProfile,
lookup: lookupProfile,
},

{
typ: "runtime",
alt: []string{"rt", "runtimes"},
Expand Down
Loading

0 comments on commit eea6192

Please sign in to comment.