You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by st...@apache.org on 2016/01/30 23:16:54 UTC

incubator-mynewt-newt git commit: add taskstats command to newtmgr. clean up unused readLen in packet code.

Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/master fd1b20289 -> a77be9b28


add taskstats command to newtmgr.  clean up unused readLen in packet code.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/a77be9b2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/a77be9b2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/a77be9b2

Branch: refs/heads/master
Commit: a77be9b28fd20d85add36785495f45b2b1b97d2c
Parents: fd1b202
Author: Sterling Hughes <st...@apache.org>
Authored: Sat Jan 30 13:56:12 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sat Jan 30 13:56:12 2016 -0800

----------------------------------------------------------------------
 newtmgr/newtmgr.go              | 77 +++++++++++++++++++++++++++++++++++-
 newtmgr/protocol/cmdrunner.go   |  3 ++
 newtmgr/protocol/nmgr.go        |  6 ++-
 newtmgr/protocol/taskstats.go   | 69 ++++++++++++++++++++++++++++++++
 newtmgr/transport/conn.go       |  1 -
 newtmgr/transport/connserial.go |  2 +-
 6 files changed, 153 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/a77be9b2/newtmgr/newtmgr.go
----------------------------------------------------------------------
diff --git a/newtmgr/newtmgr.go b/newtmgr/newtmgr.go
index 9aed1db..f3567cf 100644
--- a/newtmgr/newtmgr.go
+++ b/newtmgr/newtmgr.go
@@ -240,6 +240,80 @@ func echoCmd() *cobra.Command {
 	return echoCmd
 }
 
+func taskStatsRunCmd(cmd *cobra.Command, args []string) {
+	cpm, err := cli.NewCpMgr()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	profile, err := cpm.GetConnProfile(ConnProfileName)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	conn, err := transport.NewConn(profile)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	runner, err := protocol.NewCmdRunner(conn)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	srr, err := protocol.NewTaskStatsReadReq()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	nmr, err := srr.EncodeWriteRequest()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	if err := runner.WriteReq(nmr); err != nil {
+		nmUsage(cmd, err)
+	}
+
+	rsp, err := runner.ReadResp()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	tsrsp, err := protocol.DecodeTaskStatsReadResponse(rsp.Data)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	fmt.Printf("Return Code = %d\n", tsrsp.ReturnCode)
+	if tsrsp.ReturnCode == 0 {
+		for k, info := range tsrsp.Tasks {
+			fmt.Printf("  %s ", k)
+			fmt.Printf("(prio=%d tid=%d runtime=%d cswcnt=%d stksize=%d "+
+				"stkusage=%d last_checkin=%d next_checkin=%d)",
+				int(info["prio"].(float64)),
+				int(info["tid"].(float64)),
+				int(info["runtime"].(float64)),
+				int(info["cswcnt"].(float64)),
+				int(info["stksiz"].(float64)),
+				int(info["stkuse"].(float64)),
+				int(info["last_checkin"].(float64)),
+				int(info["next_checkin"].(float64)))
+			fmt.Printf("\n")
+		}
+	}
+}
+
+func taskStatsCmd() *cobra.Command {
+	taskStatsCmd := &cobra.Command{
+		Use:   "taskstats",
+		Short: "Read statistics from a remote endpoint",
+		Run:   taskStatsRunCmd,
+	}
+
+	return taskStatsCmd
+}
+
 func statsRunCmd(cmd *cobra.Command, args []string) {
 	cpm, err := cli.NewCpMgr()
 	if err != nil {
@@ -641,7 +715,7 @@ func fileDownloadCmd(cmd *cobra.Command, args []string) {
 	var fileSz uint32 = 1
 
 	file, err := os.OpenFile(args[1], os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
-    	if err != nil {
+	if err != nil {
 		nmUsage(cmd, util.NewNewtError(fmt.Sprintf(
 			"Cannot open file %s - %s", args[1], err.Error())))
 	}
@@ -825,6 +899,7 @@ func parseCmds() *cobra.Command {
 	nmCmd.AddCommand(echoCmd())
 	nmCmd.AddCommand(imageCmd())
 	nmCmd.AddCommand(statsCmd())
+	nmCmd.AddCommand(taskStatsCmd())
 	nmCmd.AddCommand(configCmd())
 
 	return nmCmd

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/a77be9b2/newtmgr/protocol/cmdrunner.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/cmdrunner.go b/newtmgr/protocol/cmdrunner.go
index 05b604a..8f4db62 100644
--- a/newtmgr/protocol/cmdrunner.go
+++ b/newtmgr/protocol/cmdrunner.go
@@ -32,6 +32,9 @@ func (cr *CmdRunner) ReadResp() (*NmgrReq, error) {
 			return nil, err
 		}
 
+		bytes := pkt.GetBytes()
+		bytes = bytes[8:]
+
 		nmr, err := DeserializeNmgrReq(pkt.GetBytes())
 		if err != nil {
 			return nil, err

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/a77be9b2/newtmgr/protocol/nmgr.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/nmgr.go b/newtmgr/protocol/nmgr.go
index 9954347..115df92 100644
--- a/newtmgr/protocol/nmgr.go
+++ b/newtmgr/protocol/nmgr.go
@@ -68,8 +68,10 @@ func DeserializeNmgrReq(data []byte) (*NmgrReq, error) {
 
 	data = data[8:]
 	if int(nmr.Len) != len(data) {
-		return nil, util.NewNewtError("Newtmgr request length doesn't " +
-			"match data length.")
+		return nil, util.NewNewtError(
+			fmt.Sprintf("Newtmgr request length doesn't match data length."+
+				"  Newtmgr length = %d, Data length = %d\n", nmr.Len,
+				len(data)))
 	}
 	nmr.Data = data
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/a77be9b2/newtmgr/protocol/taskstats.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/taskstats.go b/newtmgr/protocol/taskstats.go
new file mode 100644
index 0000000..5025ae7
--- /dev/null
+++ b/newtmgr/protocol/taskstats.go
@@ -0,0 +1,69 @@
+/*
+ Copyright 2015 Runtime Inc.
+ 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 protocol
+
+import (
+	"encoding/json"
+	"fmt"
+
+	"git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/util"
+)
+
+const (
+	NMGR_ID_TASKSTATS = 2
+)
+
+type TaskStatsReadReq struct {
+}
+
+type TaskStatsReadRsp struct {
+	ReturnCode int                               `json:"rc"`
+	Tasks      map[string]map[string]interface{} `json:"tasks"`
+}
+
+func NewTaskStatsReadReq() (*TaskStatsReadReq, error) {
+	s := &TaskStatsReadReq{}
+
+	return s, nil
+}
+
+func (tsr *TaskStatsReadReq) EncodeWriteRequest() (*NmgrReq, error) {
+	nmr, err := NewNmgrReq()
+	if err != nil {
+		return nil, err
+	}
+
+	nmr.Op = NMGR_OP_READ
+	nmr.Flags = 0
+	nmr.Group = NMGR_GROUP_ID_DEFAULT
+	nmr.Id = NMGR_ID_TASKSTATS
+
+	nmr.Data = []byte{}
+	nmr.Len = uint16(0)
+
+	return nmr, nil
+}
+
+func DecodeTaskStatsReadResponse(data []byte) (*TaskStatsReadRsp, error) {
+	var tsr TaskStatsReadRsp
+	err := json.Unmarshal(data, &tsr)
+	if err != nil {
+		return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming json: %s",
+			err.Error()))
+	}
+
+	return &tsr, nil
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/a77be9b2/newtmgr/transport/conn.go
----------------------------------------------------------------------
diff --git a/newtmgr/transport/conn.go b/newtmgr/transport/conn.go
index 560b349..708f2ee 100644
--- a/newtmgr/transport/conn.go
+++ b/newtmgr/transport/conn.go
@@ -30,7 +30,6 @@ type Conn interface {
 
 type Packet struct {
 	expectedLen uint16
-	readLen     uint16
 	buffer      *bytes.Buffer
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/a77be9b2/newtmgr/transport/connserial.go
----------------------------------------------------------------------
diff --git a/newtmgr/transport/connserial.go b/newtmgr/transport/connserial.go
index 1115fa4..f4880b7 100644
--- a/newtmgr/transport/connserial.go
+++ b/newtmgr/transport/connserial.go
@@ -79,7 +79,7 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
 		if err != nil {
 			return nil, util.NewNewtError(
 				fmt.Sprintf("Couldn't decode base64 string: %b",
-					line))
+					base64Data))
 		}
 
 		if line[0] == 6 && line[1] == 9 {