Generate build timing metrics to proto format file

Test: Dumped the text formated based metrics file to out dir,
and checked the file.
Bug: b/63815990

Change-Id: Iff476f72a0be74eb53b6b26ef468d11c0f24a404
diff --git a/cmd/soong_ui/main.go b/cmd/soong_ui/main.go
index b603571..2bd0cf4 100644
--- a/cmd/soong_ui/main.go
+++ b/cmd/soong_ui/main.go
@@ -26,6 +26,7 @@
 
 	"android/soong/ui/build"
 	"android/soong/ui/logger"
+	"android/soong/ui/metrics"
 	"android/soong/ui/status"
 	"android/soong/ui/terminal"
 	"android/soong/ui/tracer"
@@ -73,6 +74,8 @@
 	trace := tracer.New(log)
 	defer trace.Close()
 
+	met := metrics.New()
+
 	stat := &status.Status{}
 	defer stat.Finish()
 	stat.AddOutput(terminal.NewStatusOutput(writer, os.Getenv("NINJA_STATUS")))
@@ -87,6 +90,7 @@
 	buildCtx := build.Context{ContextImpl: &build.ContextImpl{
 		Context: ctx,
 		Logger:  log,
+		Metrics: met,
 		Tracer:  trace,
 		Writer:  writer,
 		Status:  stat,
@@ -100,6 +104,9 @@
 
 	build.SetupOutDir(buildCtx, config)
 
+	metricsPath := filepath.Join(config.OutDir(), "build_metrics")
+	defer met.Dump(metricsPath)
+
 	logsDir := config.OutDir()
 	if config.Dist() {
 		logsDir = filepath.Join(config.DistDir(), "logs")
@@ -116,7 +123,7 @@
 			if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
 				log.Verbosef("Took %dms to start up.",
 					time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
-				buildCtx.CompleteTrace("startup", start_time, uint64(time.Now().UnixNano()))
+				buildCtx.CompleteTrace(metrics.RunSetupTool, "startup", start_time, uint64(time.Now().UnixNano()))
 			}
 		}
 
diff --git a/ui/build/Android.bp b/ui/build/Android.bp
index a48a314..0eba5ce 100644
--- a/ui/build/Android.bp
+++ b/ui/build/Android.bp
@@ -30,6 +30,7 @@
     deps: [
         "soong-ui-build-paths",
         "soong-ui-logger",
+        "soong-ui-metrics",
         "soong-ui-status",
         "soong-ui-terminal",
         "soong-ui-tracer",
diff --git a/ui/build/cleanbuild.go b/ui/build/cleanbuild.go
index 4dee638..c47f614 100644
--- a/ui/build/cleanbuild.go
+++ b/ui/build/cleanbuild.go
@@ -20,6 +20,8 @@
 	"os"
 	"path/filepath"
 	"strings"
+
+	"android/soong/ui/metrics"
 )
 
 func removeGlobs(ctx Context, globs ...string) {
@@ -158,7 +160,7 @@
 		return
 	}
 
-	ctx.BeginTrace("installclean")
+	ctx.BeginTrace(metrics.PrimaryNinja, "installclean")
 	defer ctx.EndTrace()
 
 	prevConfig := strings.TrimPrefix(strings.TrimSuffix(string(prev), suffix), prefix)
diff --git a/ui/build/config.go b/ui/build/config.go
index 5a6d5db..97b009a 100644
--- a/ui/build/config.go
+++ b/ui/build/config.go
@@ -217,6 +217,9 @@
 	} else {
 		content = strconv.FormatInt(time.Now().Unix(), 10)
 	}
+	if ctx.Metrics != nil {
+		ctx.Metrics.SetBuildDateTime(content)
+	}
 	err := ioutil.WriteFile(buildDateTimeFile, []byte(content), 0777)
 	if err != nil {
 		ctx.Fatalln("Failed to write BUILD_DATETIME to file:", err)
diff --git a/ui/build/context.go b/ui/build/context.go
index c8b00c3..249e898 100644
--- a/ui/build/context.go
+++ b/ui/build/context.go
@@ -18,6 +18,8 @@
 	"context"
 
 	"android/soong/ui/logger"
+	"android/soong/ui/metrics"
+	"android/soong/ui/metrics/metrics_proto"
 	"android/soong/ui/status"
 	"android/soong/ui/terminal"
 	"android/soong/ui/tracer"
@@ -31,6 +33,8 @@
 	context.Context
 	logger.Logger
 
+	Metrics *metrics.Metrics
+
 	Writer terminal.Writer
 	Status *status.Status
 
@@ -39,9 +43,12 @@
 }
 
 // BeginTrace starts a new Duration Event.
-func (c ContextImpl) BeginTrace(name string) {
+func (c ContextImpl) BeginTrace(name, desc string) {
 	if c.Tracer != nil {
-		c.Tracer.Begin(name, c.Thread)
+		c.Tracer.Begin(desc, c.Thread)
+	}
+	if c.Metrics != nil {
+		c.Metrics.TimeTracer.Begin(name, desc, c.Thread)
 	}
 }
 
@@ -50,11 +57,23 @@
 	if c.Tracer != nil {
 		c.Tracer.End(c.Thread)
 	}
+	if c.Metrics != nil {
+		c.Metrics.SetTimeMetrics(c.Metrics.TimeTracer.End(c.Thread))
+	}
 }
 
 // CompleteTrace writes a trace with a beginning and end times.
-func (c ContextImpl) CompleteTrace(name string, begin, end uint64) {
+func (c ContextImpl) CompleteTrace(name, desc string, begin, end uint64) {
 	if c.Tracer != nil {
-		c.Tracer.Complete(name, c.Thread, begin, end)
+		c.Tracer.Complete(desc, c.Thread, begin, end)
+	}
+	if c.Metrics != nil {
+		realTime := end - begin
+		c.Metrics.SetTimeMetrics(
+			metrics_proto.PerfInfo{
+				Desc:      &desc,
+				Name:      &name,
+				StartTime: &begin,
+				RealTime:  &realTime})
 	}
 }
diff --git a/ui/build/dumpvars.go b/ui/build/dumpvars.go
index ad57d02..1ab855d 100644
--- a/ui/build/dumpvars.go
+++ b/ui/build/dumpvars.go
@@ -19,6 +19,7 @@
 	"fmt"
 	"strings"
 
+	"android/soong/ui/metrics"
 	"android/soong/ui/status"
 )
 
@@ -69,7 +70,7 @@
 }
 
 func dumpMakeVars(ctx Context, config Config, goals, vars []string, write_soong_vars bool) (map[string]string, error) {
-	ctx.BeginTrace("dumpvars")
+	ctx.BeginTrace(metrics.RunKati, "dumpvars")
 	defer ctx.EndTrace()
 
 	cmd := Command(ctx, config, "dumpvars",
@@ -113,6 +114,9 @@
 			return nil, fmt.Errorf("Failed to parse make line: %q", line)
 		}
 	}
+	if ctx.Metrics != nil {
+		ctx.Metrics.SetMetadataMetrics(ret)
+	}
 
 	return ret, nil
 }
diff --git a/ui/build/finder.go b/ui/build/finder.go
index 3130f74..0f34b5e 100644
--- a/ui/build/finder.go
+++ b/ui/build/finder.go
@@ -23,6 +23,8 @@
 	"os"
 	"path/filepath"
 	"strings"
+
+	"android/soong/ui/metrics"
 )
 
 // This file provides an interface to the Finder for use in Soong UI
@@ -31,7 +33,7 @@
 // NewSourceFinder returns a new Finder configured to search for source files.
 // Callers of NewSourceFinder should call <f.Shutdown()> when done
 func NewSourceFinder(ctx Context, config Config) (f *finder.Finder) {
-	ctx.BeginTrace("find modules")
+	ctx.BeginTrace(metrics.RunSetupTool, "find modules")
 	defer ctx.EndTrace()
 
 	dir, err := os.Getwd()
diff --git a/ui/build/kati.go b/ui/build/kati.go
index d2e9fe9..205d5ae 100644
--- a/ui/build/kati.go
+++ b/ui/build/kati.go
@@ -21,6 +21,7 @@
 	"path/filepath"
 	"strings"
 
+	"android/soong/ui/metrics"
 	"android/soong/ui/status"
 )
 
@@ -101,7 +102,7 @@
 }
 
 func runKatiBuild(ctx Context, config Config) {
-	ctx.BeginTrace("kati build")
+	ctx.BeginTrace(metrics.RunKati, "kati build")
 	defer ctx.EndTrace()
 
 	args := []string{
@@ -137,7 +138,7 @@
 }
 
 func runKatiPackage(ctx Context, config Config) {
-	ctx.BeginTrace("kati package")
+	ctx.BeginTrace(metrics.RunKati, "kati package")
 	defer ctx.EndTrace()
 
 	args := []string{
@@ -178,7 +179,7 @@
 }
 
 func runKatiCleanSpec(ctx Context, config Config) {
-	ctx.BeginTrace("kati cleanspec")
+	ctx.BeginTrace(metrics.RunKati, "kati cleanspec")
 	defer ctx.EndTrace()
 
 	runKati(ctx, config, katiCleanspecSuffix, []string{
diff --git a/ui/build/ninja.go b/ui/build/ninja.go
index ab15e86..e5d6da1 100644
--- a/ui/build/ninja.go
+++ b/ui/build/ninja.go
@@ -22,11 +22,12 @@
 	"strings"
 	"time"
 
+	"android/soong/ui/metrics"
 	"android/soong/ui/status"
 )
 
 func runNinja(ctx Context, config Config) {
-	ctx.BeginTrace("ninja")
+	ctx.BeginTrace(metrics.PrimaryNinja, "ninja")
 	defer ctx.EndTrace()
 
 	fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
diff --git a/ui/build/path.go b/ui/build/path.go
index 8260ff9..ee72cfd 100644
--- a/ui/build/path.go
+++ b/ui/build/path.go
@@ -25,6 +25,7 @@
 	"github.com/google/blueprint/microfactory"
 
 	"android/soong/ui/build/paths"
+	"android/soong/ui/metrics"
 )
 
 func parsePathDir(dir string) []string {
@@ -57,7 +58,7 @@
 		return
 	}
 
-	ctx.BeginTrace("path")
+	ctx.BeginTrace(metrics.RunSetupTool, "path")
 	defer ctx.EndTrace()
 
 	origPath, _ := config.Environment().Get("PATH")
diff --git a/ui/build/soong.go b/ui/build/soong.go
index 478c02c..c89f0d5 100644
--- a/ui/build/soong.go
+++ b/ui/build/soong.go
@@ -22,15 +22,16 @@
 
 	"github.com/google/blueprint/microfactory"
 
+	"android/soong/ui/metrics"
 	"android/soong/ui/status"
 )
 
 func runSoong(ctx Context, config Config) {
-	ctx.BeginTrace("soong")
+	ctx.BeginTrace(metrics.RunSoong, "soong")
 	defer ctx.EndTrace()
 
 	func() {
-		ctx.BeginTrace("blueprint bootstrap")
+		ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap")
 		defer ctx.EndTrace()
 
 		cmd := Command(ctx, config, "blueprint bootstrap", "build/blueprint/bootstrap.bash", "-t")
@@ -48,7 +49,7 @@
 	}()
 
 	func() {
-		ctx.BeginTrace("environment check")
+		ctx.BeginTrace(metrics.RunSoong, "environment check")
 		defer ctx.EndTrace()
 
 		envFile := filepath.Join(config.SoongOutDir(), ".soong.environment")
@@ -84,7 +85,7 @@
 	cfg.TrimPath = absPath(ctx, ".")
 
 	func() {
-		ctx.BeginTrace("minibp")
+		ctx.BeginTrace(metrics.RunSoong, "minibp")
 		defer ctx.EndTrace()
 
 		minibp := filepath.Join(config.SoongOutDir(), ".minibootstrap/minibp")
@@ -94,7 +95,7 @@
 	}()
 
 	func() {
-		ctx.BeginTrace("bpglob")
+		ctx.BeginTrace(metrics.RunSoong, "bpglob")
 		defer ctx.EndTrace()
 
 		bpglob := filepath.Join(config.SoongOutDir(), ".minibootstrap/bpglob")
@@ -104,7 +105,7 @@
 	}()
 
 	ninja := func(name, file string) {
-		ctx.BeginTrace(name)
+		ctx.BeginTrace(metrics.RunSoong, name)
 		defer ctx.EndTrace()
 
 		fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
diff --git a/ui/build/test_build.go b/ui/build/test_build.go
index 348d41f..5109465 100644
--- a/ui/build/test_build.go
+++ b/ui/build/test_build.go
@@ -22,6 +22,7 @@
 	"sort"
 	"strings"
 
+	"android/soong/ui/metrics"
 	"android/soong/ui/status"
 )
 
@@ -37,7 +38,7 @@
 		return
 	}
 
-	ctx.BeginTrace("test for dangling rules")
+	ctx.BeginTrace(metrics.TestRun, "test for dangling rules")
 	defer ctx.EndTrace()
 
 	ts := ctx.Status.StartTool()
diff --git a/ui/metrics/Android.bp b/ui/metrics/Android.bp
new file mode 100644
index 0000000..529639d
--- /dev/null
+++ b/ui/metrics/Android.bp
@@ -0,0 +1,37 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+bootstrap_go_package {
+    name: "soong-ui-metrics",
+    pkgPath: "android/soong/ui/metrics",
+    deps: [
+        "golang-protobuf-proto",
+        "soong-ui-metrics_proto",
+        "soong-ui-tracer",
+    ],
+    srcs: [
+        "metrics.go",
+        "time.go",
+    ],
+}
+
+bootstrap_go_package {
+    name: "soong-ui-metrics_proto",
+    pkgPath: "android/soong/ui/metrics/metrics_proto",
+    deps: ["golang-protobuf-proto"],
+    srcs: [
+        "metrics_proto/metrics.pb.go",
+    ],
+}
+
diff --git a/ui/metrics/metrics.go b/ui/metrics/metrics.go
new file mode 100644
index 0000000..790b67a
--- /dev/null
+++ b/ui/metrics/metrics.go
@@ -0,0 +1,161 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package metrics
+
+import (
+	"io/ioutil"
+	"os"
+	"strconv"
+
+	"android/soong/ui/metrics/metrics_proto"
+
+	"github.com/golang/protobuf/proto"
+)
+
+const (
+	RunSetupTool = "setup"
+	RunKati      = "kati"
+	RunSoong     = "soong"
+	PrimaryNinja = "ninja"
+	TestRun      = "test"
+)
+
+type Metrics struct {
+	metrics    metrics_proto.MetricsBase
+	TimeTracer TimeTracer
+}
+
+func New() (metrics *Metrics) {
+	m := &Metrics{
+		metrics:    metrics_proto.MetricsBase{},
+		TimeTracer: &timeTracerImpl{},
+	}
+	return m
+}
+
+func (m *Metrics) SetTimeMetrics(perf metrics_proto.PerfInfo) {
+	switch perf.GetName() {
+	case RunKati:
+		m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf)
+		break
+	case RunSoong:
+		m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf)
+		break
+	case PrimaryNinja:
+		m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf)
+		break
+	default:
+		// ignored
+	}
+}
+
+func (m *Metrics) SetMetadataMetrics(metadata map[string]string) {
+	for k, v := range metadata {
+		switch k {
+		case "BUILD_ID":
+			m.metrics.BuildId = proto.String(v)
+			break
+		case "PLATFORM_VERSION_CODENAME":
+			m.metrics.PlatformVersionCodename = proto.String(v)
+			break
+		case "TARGET_PRODUCT":
+			m.metrics.TargetProduct = proto.String(v)
+			break
+		case "TARGET_BUILD_VARIANT":
+			switch v {
+			case "user":
+				m.metrics.TargetBuildVariant = metrics_proto.MetricsBase_USER.Enum()
+			case "userdebug":
+				m.metrics.TargetBuildVariant = metrics_proto.MetricsBase_USERDEBUG.Enum()
+			case "eng":
+				m.metrics.TargetBuildVariant = metrics_proto.MetricsBase_ENG.Enum()
+			default:
+				// ignored
+			}
+		case "TARGET_ARCH":
+			m.metrics.TargetArch = m.getArch(v)
+		case "TARGET_ARCH_VARIANT":
+			m.metrics.TargetArchVariant = proto.String(v)
+		case "TARGET_CPU_VARIANT":
+			m.metrics.TargetCpuVariant = proto.String(v)
+		case "HOST_ARCH":
+			m.metrics.HostArch = m.getArch(v)
+		case "HOST_2ND_ARCH":
+			m.metrics.Host_2NdArch = m.getArch(v)
+		case "HOST_OS":
+			m.metrics.HostOs = proto.String(v)
+		case "HOST_OS_EXTRA":
+			m.metrics.HostOsExtra = proto.String(v)
+		case "HOST_CROSS_OS":
+			m.metrics.HostCrossOs = proto.String(v)
+		case "HOST_CROSS_ARCH":
+			m.metrics.HostCrossArch = proto.String(v)
+		case "HOST_CROSS_2ND_ARCH":
+			m.metrics.HostCross_2NdArch = proto.String(v)
+		case "OUT_DIR":
+			m.metrics.OutDir = proto.String(v)
+		default:
+			// ignored
+		}
+	}
+}
+
+func (m *Metrics) getArch(arch string) *metrics_proto.MetricsBase_ARCH {
+	switch arch {
+	case "arm":
+		return metrics_proto.MetricsBase_ARM.Enum()
+	case "arm64":
+		return metrics_proto.MetricsBase_ARM64.Enum()
+	case "x86":
+		return metrics_proto.MetricsBase_X86.Enum()
+	case "x86_64":
+		return metrics_proto.MetricsBase_X86_64.Enum()
+	default:
+		return metrics_proto.MetricsBase_UNKNOWN.Enum()
+	}
+}
+
+func (m *Metrics) SetBuildDateTime(date_time string) {
+	if date_time != "" {
+		date_time_timestamp, err := strconv.ParseInt(date_time, 10, 64)
+		if err != nil {
+			panic(err)
+		}
+		m.metrics.BuildDateTimestamp = &date_time_timestamp
+	}
+}
+
+func (m *Metrics) Serialize() (data []byte, err error) {
+	return proto.Marshal(&m.metrics)
+}
+
+// exports the output to the file at outputPath
+func (m *Metrics) Dump(outputPath string) (err error) {
+	data, err := m.Serialize()
+	if err != nil {
+		return err
+	}
+	tempPath := outputPath + ".tmp"
+	err = ioutil.WriteFile(tempPath, []byte(data), 0777)
+	if err != nil {
+		return err
+	}
+	err = os.Rename(tempPath, outputPath)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
diff --git a/ui/metrics/metrics_proto/metrics.pb.go b/ui/metrics/metrics_proto/metrics.pb.go
new file mode 100644
index 0000000..feefc89
--- /dev/null
+++ b/ui/metrics/metrics_proto/metrics.pb.go
@@ -0,0 +1,557 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: metrics.proto
+
+package metrics_proto
+
+import proto "github.com/golang/protobuf/proto"
+import fmt "fmt"
+import math "math"
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the proto package it is being compiled against.
+// A compilation error at this line likely means your copy of the
+// proto package needs to be updated.
+const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
+
+type MetricsBase_BUILDVARIANT int32
+
+const (
+	MetricsBase_USER      MetricsBase_BUILDVARIANT = 0
+	MetricsBase_USERDEBUG MetricsBase_BUILDVARIANT = 1
+	MetricsBase_ENG       MetricsBase_BUILDVARIANT = 2
+)
+
+var MetricsBase_BUILDVARIANT_name = map[int32]string{
+	0: "USER",
+	1: "USERDEBUG",
+	2: "ENG",
+}
+var MetricsBase_BUILDVARIANT_value = map[string]int32{
+	"USER":      0,
+	"USERDEBUG": 1,
+	"ENG":       2,
+}
+
+func (x MetricsBase_BUILDVARIANT) Enum() *MetricsBase_BUILDVARIANT {
+	p := new(MetricsBase_BUILDVARIANT)
+	*p = x
+	return p
+}
+func (x MetricsBase_BUILDVARIANT) String() string {
+	return proto.EnumName(MetricsBase_BUILDVARIANT_name, int32(x))
+}
+func (x *MetricsBase_BUILDVARIANT) UnmarshalJSON(data []byte) error {
+	value, err := proto.UnmarshalJSONEnum(MetricsBase_BUILDVARIANT_value, data, "MetricsBase_BUILDVARIANT")
+	if err != nil {
+		return err
+	}
+	*x = MetricsBase_BUILDVARIANT(value)
+	return nil
+}
+func (MetricsBase_BUILDVARIANT) EnumDescriptor() ([]byte, []int) {
+	return fileDescriptor_metrics_9e7b895801991242, []int{0, 0}
+}
+
+type MetricsBase_ARCH int32
+
+const (
+	MetricsBase_UNKNOWN MetricsBase_ARCH = 0
+	MetricsBase_ARM     MetricsBase_ARCH = 1
+	MetricsBase_ARM64   MetricsBase_ARCH = 2
+	MetricsBase_X86     MetricsBase_ARCH = 3
+	MetricsBase_X86_64  MetricsBase_ARCH = 4
+)
+
+var MetricsBase_ARCH_name = map[int32]string{
+	0: "UNKNOWN",
+	1: "ARM",
+	2: "ARM64",
+	3: "X86",
+	4: "X86_64",
+}
+var MetricsBase_ARCH_value = map[string]int32{
+	"UNKNOWN": 0,
+	"ARM":     1,
+	"ARM64":   2,
+	"X86":     3,
+	"X86_64":  4,
+}
+
+func (x MetricsBase_ARCH) Enum() *MetricsBase_ARCH {
+	p := new(MetricsBase_ARCH)
+	*p = x
+	return p
+}
+func (x MetricsBase_ARCH) String() string {
+	return proto.EnumName(MetricsBase_ARCH_name, int32(x))
+}
+func (x *MetricsBase_ARCH) UnmarshalJSON(data []byte) error {
+	value, err := proto.UnmarshalJSONEnum(MetricsBase_ARCH_value, data, "MetricsBase_ARCH")
+	if err != nil {
+		return err
+	}
+	*x = MetricsBase_ARCH(value)
+	return nil
+}
+func (MetricsBase_ARCH) EnumDescriptor() ([]byte, []int) {
+	return fileDescriptor_metrics_9e7b895801991242, []int{0, 1}
+}
+
+type ModuleTypeInfo_BUILDSYSTEM int32
+
+const (
+	ModuleTypeInfo_UNKNOWN ModuleTypeInfo_BUILDSYSTEM = 0
+	ModuleTypeInfo_SOONG   ModuleTypeInfo_BUILDSYSTEM = 1
+	ModuleTypeInfo_MAKE    ModuleTypeInfo_BUILDSYSTEM = 2
+)
+
+var ModuleTypeInfo_BUILDSYSTEM_name = map[int32]string{
+	0: "UNKNOWN",
+	1: "SOONG",
+	2: "MAKE",
+}
+var ModuleTypeInfo_BUILDSYSTEM_value = map[string]int32{
+	"UNKNOWN": 0,
+	"SOONG":   1,
+	"MAKE":    2,
+}
+
+func (x ModuleTypeInfo_BUILDSYSTEM) Enum() *ModuleTypeInfo_BUILDSYSTEM {
+	p := new(ModuleTypeInfo_BUILDSYSTEM)
+	*p = x
+	return p
+}
+func (x ModuleTypeInfo_BUILDSYSTEM) String() string {
+	return proto.EnumName(ModuleTypeInfo_BUILDSYSTEM_name, int32(x))
+}
+func (x *ModuleTypeInfo_BUILDSYSTEM) UnmarshalJSON(data []byte) error {
+	value, err := proto.UnmarshalJSONEnum(ModuleTypeInfo_BUILDSYSTEM_value, data, "ModuleTypeInfo_BUILDSYSTEM")
+	if err != nil {
+		return err
+	}
+	*x = ModuleTypeInfo_BUILDSYSTEM(value)
+	return nil
+}
+func (ModuleTypeInfo_BUILDSYSTEM) EnumDescriptor() ([]byte, []int) {
+	return fileDescriptor_metrics_9e7b895801991242, []int{2, 0}
+}
+
+type MetricsBase struct {
+	// Timestamp generated when the build starts.
+	BuildDateTimestamp *int64 `protobuf:"varint,1,opt,name=build_date_timestamp,json=buildDateTimestamp" json:"build_date_timestamp,omitempty"`
+	// It is usually used to specify the branch name [and release candidate].
+	BuildId *string `protobuf:"bytes,2,opt,name=build_id,json=buildId" json:"build_id,omitempty"`
+	// The platform version codename, eg. P, Q, REL.
+	PlatformVersionCodename *string `protobuf:"bytes,3,opt,name=platform_version_codename,json=platformVersionCodename" json:"platform_version_codename,omitempty"`
+	// The target product information, eg. aosp_arm.
+	TargetProduct *string `protobuf:"bytes,4,opt,name=target_product,json=targetProduct" json:"target_product,omitempty"`
+	// The target build variant information, eg. eng.
+	TargetBuildVariant *MetricsBase_BUILDVARIANT `protobuf:"varint,5,opt,name=target_build_variant,json=targetBuildVariant,enum=build_metrics.MetricsBase_BUILDVARIANT,def=2" json:"target_build_variant,omitempty"`
+	// The target arch information, eg. arm.
+	TargetArch *MetricsBase_ARCH `protobuf:"varint,6,opt,name=target_arch,json=targetArch,enum=build_metrics.MetricsBase_ARCH,def=0" json:"target_arch,omitempty"`
+	// The target arch variant information, eg. armv7-a-neon.
+	TargetArchVariant *string `protobuf:"bytes,7,opt,name=target_arch_variant,json=targetArchVariant" json:"target_arch_variant,omitempty"`
+	// The target cpu variant information, eg. generic.
+	TargetCpuVariant *string `protobuf:"bytes,8,opt,name=target_cpu_variant,json=targetCpuVariant" json:"target_cpu_variant,omitempty"`
+	// The host arch information, eg. x86_64.
+	HostArch *MetricsBase_ARCH `protobuf:"varint,9,opt,name=host_arch,json=hostArch,enum=build_metrics.MetricsBase_ARCH,def=0" json:"host_arch,omitempty"`
+	// The host 2nd arch information, eg. x86.
+	Host_2NdArch *MetricsBase_ARCH `protobuf:"varint,10,opt,name=host_2nd_arch,json=host2ndArch,enum=build_metrics.MetricsBase_ARCH,def=0" json:"host_2nd_arch,omitempty"`
+	// The host os information, eg. linux.
+	HostOs *string `protobuf:"bytes,11,opt,name=host_os,json=hostOs" json:"host_os,omitempty"`
+	// The host os extra information, eg. Linux-4.17.0-3rodete2-amd64-x86_64-Debian-GNU.
+	HostOsExtra *string `protobuf:"bytes,12,opt,name=host_os_extra,json=hostOsExtra" json:"host_os_extra,omitempty"`
+	// The host cross os information, eg. windows.
+	HostCrossOs *string `protobuf:"bytes,13,opt,name=host_cross_os,json=hostCrossOs" json:"host_cross_os,omitempty"`
+	// The host cross arch information, eg. x86.
+	HostCrossArch *string `protobuf:"bytes,14,opt,name=host_cross_arch,json=hostCrossArch" json:"host_cross_arch,omitempty"`
+	// The host cross 2nd arch information, eg. x86_64.
+	HostCross_2NdArch *string `protobuf:"bytes,15,opt,name=host_cross_2nd_arch,json=hostCross2ndArch" json:"host_cross_2nd_arch,omitempty"`
+	// The directory for generated built artifacts installation, eg. out.
+	OutDir *string `protobuf:"bytes,16,opt,name=out_dir,json=outDir" json:"out_dir,omitempty"`
+	// The metrics for calling various tools (microfactory) before Soong_UI starts.
+	SetupTools []*PerfInfo `protobuf:"bytes,17,rep,name=setup_tools,json=setupTools" json:"setup_tools,omitempty"`
+	// The metrics for calling Kati by multiple times.
+	KatiRuns []*PerfInfo `protobuf:"bytes,18,rep,name=kati_runs,json=katiRuns" json:"kati_runs,omitempty"`
+	// The metrics for calling Soong.
+	SoongRuns []*PerfInfo `protobuf:"bytes,19,rep,name=soong_runs,json=soongRuns" json:"soong_runs,omitempty"`
+	// The metrics for calling Ninja.
+	NinjaRuns            []*PerfInfo `protobuf:"bytes,20,rep,name=ninja_runs,json=ninjaRuns" json:"ninja_runs,omitempty"`
+	XXX_NoUnkeyedLiteral struct{}    `json:"-"`
+	XXX_unrecognized     []byte      `json:"-"`
+	XXX_sizecache        int32       `json:"-"`
+}
+
+func (m *MetricsBase) Reset()         { *m = MetricsBase{} }
+func (m *MetricsBase) String() string { return proto.CompactTextString(m) }
+func (*MetricsBase) ProtoMessage()    {}
+func (*MetricsBase) Descriptor() ([]byte, []int) {
+	return fileDescriptor_metrics_9e7b895801991242, []int{0}
+}
+func (m *MetricsBase) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_MetricsBase.Unmarshal(m, b)
+}
+func (m *MetricsBase) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_MetricsBase.Marshal(b, m, deterministic)
+}
+func (dst *MetricsBase) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_MetricsBase.Merge(dst, src)
+}
+func (m *MetricsBase) XXX_Size() int {
+	return xxx_messageInfo_MetricsBase.Size(m)
+}
+func (m *MetricsBase) XXX_DiscardUnknown() {
+	xxx_messageInfo_MetricsBase.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_MetricsBase proto.InternalMessageInfo
+
+const Default_MetricsBase_TargetBuildVariant MetricsBase_BUILDVARIANT = MetricsBase_ENG
+const Default_MetricsBase_TargetArch MetricsBase_ARCH = MetricsBase_UNKNOWN
+const Default_MetricsBase_HostArch MetricsBase_ARCH = MetricsBase_UNKNOWN
+const Default_MetricsBase_Host_2NdArch MetricsBase_ARCH = MetricsBase_UNKNOWN
+
+func (m *MetricsBase) GetBuildDateTimestamp() int64 {
+	if m != nil && m.BuildDateTimestamp != nil {
+		return *m.BuildDateTimestamp
+	}
+	return 0
+}
+
+func (m *MetricsBase) GetBuildId() string {
+	if m != nil && m.BuildId != nil {
+		return *m.BuildId
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetPlatformVersionCodename() string {
+	if m != nil && m.PlatformVersionCodename != nil {
+		return *m.PlatformVersionCodename
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetTargetProduct() string {
+	if m != nil && m.TargetProduct != nil {
+		return *m.TargetProduct
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetTargetBuildVariant() MetricsBase_BUILDVARIANT {
+	if m != nil && m.TargetBuildVariant != nil {
+		return *m.TargetBuildVariant
+	}
+	return Default_MetricsBase_TargetBuildVariant
+}
+
+func (m *MetricsBase) GetTargetArch() MetricsBase_ARCH {
+	if m != nil && m.TargetArch != nil {
+		return *m.TargetArch
+	}
+	return Default_MetricsBase_TargetArch
+}
+
+func (m *MetricsBase) GetTargetArchVariant() string {
+	if m != nil && m.TargetArchVariant != nil {
+		return *m.TargetArchVariant
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetTargetCpuVariant() string {
+	if m != nil && m.TargetCpuVariant != nil {
+		return *m.TargetCpuVariant
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetHostArch() MetricsBase_ARCH {
+	if m != nil && m.HostArch != nil {
+		return *m.HostArch
+	}
+	return Default_MetricsBase_HostArch
+}
+
+func (m *MetricsBase) GetHost_2NdArch() MetricsBase_ARCH {
+	if m != nil && m.Host_2NdArch != nil {
+		return *m.Host_2NdArch
+	}
+	return Default_MetricsBase_Host_2NdArch
+}
+
+func (m *MetricsBase) GetHostOs() string {
+	if m != nil && m.HostOs != nil {
+		return *m.HostOs
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetHostOsExtra() string {
+	if m != nil && m.HostOsExtra != nil {
+		return *m.HostOsExtra
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetHostCrossOs() string {
+	if m != nil && m.HostCrossOs != nil {
+		return *m.HostCrossOs
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetHostCrossArch() string {
+	if m != nil && m.HostCrossArch != nil {
+		return *m.HostCrossArch
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetHostCross_2NdArch() string {
+	if m != nil && m.HostCross_2NdArch != nil {
+		return *m.HostCross_2NdArch
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetOutDir() string {
+	if m != nil && m.OutDir != nil {
+		return *m.OutDir
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetSetupTools() []*PerfInfo {
+	if m != nil {
+		return m.SetupTools
+	}
+	return nil
+}
+
+func (m *MetricsBase) GetKatiRuns() []*PerfInfo {
+	if m != nil {
+		return m.KatiRuns
+	}
+	return nil
+}
+
+func (m *MetricsBase) GetSoongRuns() []*PerfInfo {
+	if m != nil {
+		return m.SoongRuns
+	}
+	return nil
+}
+
+func (m *MetricsBase) GetNinjaRuns() []*PerfInfo {
+	if m != nil {
+		return m.NinjaRuns
+	}
+	return nil
+}
+
+type PerfInfo struct {
+	// The description for the phase/action/part while the tool running.
+	Desc *string `protobuf:"bytes,1,opt,name=desc" json:"desc,omitempty"`
+	// The name for the running phase/action/part.
+	Name *string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"`
+	// The absolute start time.
+	// The number of nanoseconds elapsed since January 1, 1970 UTC.
+	StartTime *uint64 `protobuf:"varint,3,opt,name=start_time,json=startTime" json:"start_time,omitempty"`
+	// The real running time.
+	// The number of nanoseconds elapsed since start_time.
+	RealTime *uint64 `protobuf:"varint,4,opt,name=real_time,json=realTime" json:"real_time,omitempty"`
+	// The number of MB for memory use.
+	MemoryUse            *uint64  `protobuf:"varint,5,opt,name=memory_use,json=memoryUse" json:"memory_use,omitempty"`
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *PerfInfo) Reset()         { *m = PerfInfo{} }
+func (m *PerfInfo) String() string { return proto.CompactTextString(m) }
+func (*PerfInfo) ProtoMessage()    {}
+func (*PerfInfo) Descriptor() ([]byte, []int) {
+	return fileDescriptor_metrics_9e7b895801991242, []int{1}
+}
+func (m *PerfInfo) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_PerfInfo.Unmarshal(m, b)
+}
+func (m *PerfInfo) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_PerfInfo.Marshal(b, m, deterministic)
+}
+func (dst *PerfInfo) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_PerfInfo.Merge(dst, src)
+}
+func (m *PerfInfo) XXX_Size() int {
+	return xxx_messageInfo_PerfInfo.Size(m)
+}
+func (m *PerfInfo) XXX_DiscardUnknown() {
+	xxx_messageInfo_PerfInfo.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_PerfInfo proto.InternalMessageInfo
+
+func (m *PerfInfo) GetDesc() string {
+	if m != nil && m.Desc != nil {
+		return *m.Desc
+	}
+	return ""
+}
+
+func (m *PerfInfo) GetName() string {
+	if m != nil && m.Name != nil {
+		return *m.Name
+	}
+	return ""
+}
+
+func (m *PerfInfo) GetStartTime() uint64 {
+	if m != nil && m.StartTime != nil {
+		return *m.StartTime
+	}
+	return 0
+}
+
+func (m *PerfInfo) GetRealTime() uint64 {
+	if m != nil && m.RealTime != nil {
+		return *m.RealTime
+	}
+	return 0
+}
+
+func (m *PerfInfo) GetMemoryUse() uint64 {
+	if m != nil && m.MemoryUse != nil {
+		return *m.MemoryUse
+	}
+	return 0
+}
+
+type ModuleTypeInfo struct {
+	// The build system, eg. Soong or Make.
+	BuildSystem *ModuleTypeInfo_BUILDSYSTEM `protobuf:"varint,1,opt,name=build_system,json=buildSystem,enum=build_metrics.ModuleTypeInfo_BUILDSYSTEM,def=0" json:"build_system,omitempty"`
+	// The module type, eg. java_library, cc_binary, and etc.
+	ModuleType *string `protobuf:"bytes,2,opt,name=module_type,json=moduleType" json:"module_type,omitempty"`
+	// The number of logical modules.
+	NumOfModules         *uint32  `protobuf:"varint,3,opt,name=num_of_modules,json=numOfModules" json:"num_of_modules,omitempty"`
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *ModuleTypeInfo) Reset()         { *m = ModuleTypeInfo{} }
+func (m *ModuleTypeInfo) String() string { return proto.CompactTextString(m) }
+func (*ModuleTypeInfo) ProtoMessage()    {}
+func (*ModuleTypeInfo) Descriptor() ([]byte, []int) {
+	return fileDescriptor_metrics_9e7b895801991242, []int{2}
+}
+func (m *ModuleTypeInfo) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_ModuleTypeInfo.Unmarshal(m, b)
+}
+func (m *ModuleTypeInfo) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_ModuleTypeInfo.Marshal(b, m, deterministic)
+}
+func (dst *ModuleTypeInfo) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_ModuleTypeInfo.Merge(dst, src)
+}
+func (m *ModuleTypeInfo) XXX_Size() int {
+	return xxx_messageInfo_ModuleTypeInfo.Size(m)
+}
+func (m *ModuleTypeInfo) XXX_DiscardUnknown() {
+	xxx_messageInfo_ModuleTypeInfo.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_ModuleTypeInfo proto.InternalMessageInfo
+
+const Default_ModuleTypeInfo_BuildSystem ModuleTypeInfo_BUILDSYSTEM = ModuleTypeInfo_UNKNOWN
+
+func (m *ModuleTypeInfo) GetBuildSystem() ModuleTypeInfo_BUILDSYSTEM {
+	if m != nil && m.BuildSystem != nil {
+		return *m.BuildSystem
+	}
+	return Default_ModuleTypeInfo_BuildSystem
+}
+
+func (m *ModuleTypeInfo) GetModuleType() string {
+	if m != nil && m.ModuleType != nil {
+		return *m.ModuleType
+	}
+	return ""
+}
+
+func (m *ModuleTypeInfo) GetNumOfModules() uint32 {
+	if m != nil && m.NumOfModules != nil {
+		return *m.NumOfModules
+	}
+	return 0
+}
+
+func init() {
+	proto.RegisterType((*MetricsBase)(nil), "build_metrics.MetricsBase")
+	proto.RegisterType((*PerfInfo)(nil), "build_metrics.PerfInfo")
+	proto.RegisterType((*ModuleTypeInfo)(nil), "build_metrics.ModuleTypeInfo")
+	proto.RegisterEnum("build_metrics.MetricsBase_BUILDVARIANT", MetricsBase_BUILDVARIANT_name, MetricsBase_BUILDVARIANT_value)
+	proto.RegisterEnum("build_metrics.MetricsBase_ARCH", MetricsBase_ARCH_name, MetricsBase_ARCH_value)
+	proto.RegisterEnum("build_metrics.ModuleTypeInfo_BUILDSYSTEM", ModuleTypeInfo_BUILDSYSTEM_name, ModuleTypeInfo_BUILDSYSTEM_value)
+}
+
+func init() { proto.RegisterFile("metrics.proto", fileDescriptor_metrics_9e7b895801991242) }
+
+var fileDescriptor_metrics_9e7b895801991242 = []byte{
+	// 783 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x55, 0xdd, 0x6e, 0xdb, 0x36,
+	0x14, 0xae, 0x62, 0x25, 0x96, 0x8e, 0x62, 0x57, 0x61, 0x02, 0x44, 0xc5, 0x50, 0x34, 0x30, 0xf6,
+	0x93, 0x01, 0x9b, 0x57, 0x18, 0x81, 0x11, 0x04, 0xbb, 0xb1, 0x13, 0xa3, 0x35, 0x5a, 0xdb, 0x85,
+	0x6c, 0x67, 0xdd, 0x2e, 0x46, 0x68, 0x12, 0xdd, 0x68, 0xb3, 0x44, 0x81, 0xa4, 0x8a, 0xf9, 0x21,
+	0xf6, 0x8c, 0x7b, 0x91,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 0x9e, 0x03, 0x48, 0x15, 0x09, 0x85, 0xe3, 0x09, 0xe7, 0x88, 0x1d, 0xba, 0x58, 0xd1, 0x53,
+	0x89, 0x7c, 0x05, 0xae, 0x60, 0xd1, 0xda, 0xa0, 0x36, 0xa2, 0x8e, 0x2e, 0x20, 0xf8, 0x1c, 0x20,
+	0x63, 0x19, 0x17, 0x1b, 0x5a, 0x4a, 0x86, 0x53, 0xc2, 0x0e, 0x5d, 0x53, 0x59, 0x4a, 0xd6, 0xf9,
+	0xd7, 0x82, 0xf6, 0x84, 0x27, 0xe5, 0x9a, 0x2d, 0x36, 0x05, 0xc3, 0x54, 0x4b, 0x38, 0x34, 0xef,
+	0x4d, 0x6e, 0xa4, 0x62, 0x19, 0xa6, 0x6b, 0xf7, 0xbe, 0xff, 0xfc, 0x42, 0xdc, 0x13, 0x99, 0xe1,
+	0x32, 0xff, 0x75, 0xbe, 0x18, 0x4d, 0x6a, 0x57, 0x03, 0x25, 0x73, 0xb4, 0x21, 0x2f, 0xc0, 0xcb,
+	0x50, 0x43, 0xd5, 0xa6, 0xa8, 0xfa, 0x83, 0x6c, 0x67, 0x43, 0xbe, 0x86, 0x76, 0x5e, 0x66, 0x94,
+	0xaf, 0xa8, 0x29, 0x4a, 0xec, 0xb4, 0x15, 0x1e, 0xe6, 0x65, 0x36, 0x5b, 0x99, 0xfd, 0x64, 0xe7,
+	0x27, 0xf0, 0x6a, 0x7b, 0xdd, 0xff, 0x0a, 0x2e, 0xec, 0xcf, 0x67, 0xb3, 0xa9, 0xfe, 0x5c, 0x0e,
+	0xd8, 0x93, 0xc1, 0x9b, 0x91, 0xbf, 0x37, 0x3c, 0x7a, 0xdd, 0xf8, 0xad, 0xfa, 0x25, 0x50, 0xfc,
+	0x25, 0xfc, 0x1f, 0x00, 0x00, 0xff, 0xff, 0xd4, 0x8d, 0x19, 0x89, 0x22, 0x06, 0x00, 0x00,
+}
diff --git a/ui/metrics/metrics_proto/metrics.proto b/ui/metrics/metrics_proto/metrics.proto
new file mode 100644
index 0000000..b3de2f4
--- /dev/null
+++ b/ui/metrics/metrics_proto/metrics.proto
@@ -0,0 +1,129 @@
+// Copyright 2018 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+syntax = "proto2";
+
+option optimize_for = LITE_RUNTIME;
+
+package build_metrics;
+option go_package = "metrics_proto";
+
+message MetricsBase {
+  // Timestamp generated when the build starts.
+  optional int64 build_date_timestamp = 1;
+
+  // It is usually used to specify the branch name [and release candidate].
+  optional string build_id  = 2;
+
+  // The platform version codename, eg. P, Q, REL.
+  optional string platform_version_codename = 3;
+
+  // The target product information, eg. aosp_arm.
+  optional string target_product = 4;
+
+  enum BUILDVARIANT {
+    USER = 0;
+    USERDEBUG = 1;
+    ENG = 2;
+  }
+  // The target build variant information, eg. eng.
+  optional BUILDVARIANT target_build_variant = 5 [default = ENG];
+
+  enum ARCH {
+    UNKNOWN = 0;
+    ARM = 1;
+    ARM64 = 2;
+    X86 = 3;
+    X86_64 = 4;
+  }
+  // The target arch information, eg. arm.
+  optional ARCH target_arch = 6 [default = UNKNOWN];
+
+  // The target arch variant information, eg. armv7-a-neon.
+  optional string target_arch_variant = 7;
+
+  // The target cpu variant information, eg. generic.
+  optional string target_cpu_variant = 8;
+
+  // The host arch information, eg. x86_64.
+  optional ARCH host_arch = 9 [default = UNKNOWN];
+
+  // The host 2nd arch information, eg. x86.
+  optional ARCH host_2nd_arch = 10 [default = UNKNOWN];
+
+  // The host os information, eg. linux.
+  optional string host_os = 11;
+
+  // The host os extra information, eg. Linux-4.17.0-3rodete2-amd64-x86_64-Debian-GNU.
+  optional string host_os_extra = 12;
+
+  // The host cross os information, eg. windows.
+  optional string host_cross_os = 13;
+
+  // The host cross arch information, eg. x86.
+  optional string host_cross_arch = 14;
+
+  // The host cross 2nd arch information, eg. x86_64.
+  optional string host_cross_2nd_arch = 15;
+
+  // The directory for generated built artifacts installation, eg. out.
+  optional string out_dir = 16;
+
+  // The metrics for calling various tools (microfactory) before Soong_UI starts.
+  repeated PerfInfo setup_tools = 17;
+
+  // The metrics for calling Kati by multiple times.
+  repeated PerfInfo kati_runs = 18;
+
+  // The metrics for calling Soong.
+  repeated PerfInfo soong_runs = 19;
+
+  // The metrics for calling Ninja.
+  repeated PerfInfo ninja_runs = 20;
+}
+
+message PerfInfo {
+  // The description for the phase/action/part while the tool running.
+  optional string desc = 1;
+
+  // The name for the running phase/action/part.
+  optional string name = 2;
+
+  // The absolute start time.
+  // The number of nanoseconds elapsed since January 1, 1970 UTC.
+  optional uint64 start_time = 3;
+
+  // The real running time.
+  // The number of nanoseconds elapsed since start_time.
+  optional uint64 real_time = 4;
+
+  // The number of MB for memory use.
+  optional uint64 memory_use = 5;
+}
+
+message ModuleTypeInfo {
+  enum BUILDSYSTEM {
+    UNKNOWN = 0;
+    SOONG = 1;
+    MAKE = 2;
+  }
+  // The build system, eg. Soong or Make.
+  optional BUILDSYSTEM build_system = 1 [default = UNKNOWN];
+
+  // The module type, eg. java_library, cc_binary, and etc.
+  optional string module_type = 2;
+
+  // The number of logical modules.
+  optional uint32 num_of_modules = 3;
+}
diff --git a/ui/metrics/metrics_proto/regen.sh b/ui/metrics/metrics_proto/regen.sh
new file mode 100755
index 0000000..343c638
--- /dev/null
+++ b/ui/metrics/metrics_proto/regen.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+aprotoc --go_out=paths=source_relative:. metrics.proto
diff --git a/ui/metrics/time.go b/ui/metrics/time.go
new file mode 100644
index 0000000..7e8801a
--- /dev/null
+++ b/ui/metrics/time.go
@@ -0,0 +1,71 @@
+// Copyright 2018 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package metrics
+
+import (
+	"time"
+
+	"android/soong/ui/metrics/metrics_proto"
+	"android/soong/ui/tracer"
+)
+
+type timeEvent struct {
+	desc string
+	name string
+
+	atNanos uint64 // timestamp measured in nanoseconds since the reference date
+}
+
+type TimeTracer interface {
+	Begin(name, desc string, thread tracer.Thread)
+	End(thread tracer.Thread) metrics_proto.PerfInfo
+}
+
+type timeTracerImpl struct {
+	activeEvents []timeEvent
+}
+
+var _ TimeTracer = &timeTracerImpl{}
+
+func (t *timeTracerImpl) now() uint64 {
+	return uint64(time.Now().UnixNano())
+}
+
+func (t *timeTracerImpl) Begin(name, desc string, thread tracer.Thread) {
+	t.beginAt(name, desc, t.now())
+}
+
+func (t *timeTracerImpl) beginAt(name, desc string, atNanos uint64) {
+	t.activeEvents = append(t.activeEvents, timeEvent{name: name, desc: desc, atNanos: atNanos})
+}
+
+func (t *timeTracerImpl) End(thread tracer.Thread) metrics_proto.PerfInfo {
+	return t.endAt(t.now())
+}
+
+func (t *timeTracerImpl) endAt(atNanos uint64) metrics_proto.PerfInfo {
+	if len(t.activeEvents) < 1 {
+		panic("Internal error: No pending events for endAt to end!")
+	}
+	lastEvent := t.activeEvents[len(t.activeEvents)-1]
+	t.activeEvents = t.activeEvents[:len(t.activeEvents)-1]
+	realTime := atNanos - lastEvent.atNanos
+
+	return metrics_proto.PerfInfo{
+		Desc:      &lastEvent.desc,
+		Name:      &lastEvent.name,
+		StartTime: &lastEvent.atNanos,
+		RealTime:  &realTime}
+}