get:
Show a patch.

patch:
Update a patch.

put:
Update a patch.

GET /api/patches/47370/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 47370,
    "url": "http://patches.dpdk.org/api/patches/47370/?format=api",
    "web_url": "http://patches.dpdk.org/project/dpdk/patch/20181024160311.85457-9-kevin.laatz@intel.com/",
    "project": {
        "id": 1,
        "url": "http://patches.dpdk.org/api/projects/1/?format=api",
        "name": "DPDK",
        "link_name": "dpdk",
        "list_id": "dev.dpdk.org",
        "list_email": "dev@dpdk.org",
        "web_url": "http://core.dpdk.org",
        "scm_url": "git://dpdk.org/dpdk",
        "webscm_url": "http://git.dpdk.org/dpdk",
        "list_archive_url": "https://inbox.dpdk.org/dev",
        "list_archive_url_format": "https://inbox.dpdk.org/dev/{}",
        "commit_url_format": ""
    },
    "msgid": "<20181024160311.85457-9-kevin.laatz@intel.com>",
    "list_archive_url": "https://inbox.dpdk.org/dev/20181024160311.85457-9-kevin.laatz@intel.com",
    "date": "2018-10-24T16:03:06",
    "name": "[v8,08/13] telemetry: format json response when sending stats",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": true,
    "hash": "84952857b681e09327b5641a44c51bcc929fb767",
    "submitter": {
        "id": 921,
        "url": "http://patches.dpdk.org/api/people/921/?format=api",
        "name": "Kevin Laatz",
        "email": "kevin.laatz@intel.com"
    },
    "delegate": {
        "id": 1,
        "url": "http://patches.dpdk.org/api/users/1/?format=api",
        "username": "tmonjalo",
        "first_name": "Thomas",
        "last_name": "Monjalon",
        "email": "thomas@monjalon.net"
    },
    "mbox": "http://patches.dpdk.org/project/dpdk/patch/20181024160311.85457-9-kevin.laatz@intel.com/mbox/",
    "series": [
        {
            "id": 2075,
            "url": "http://patches.dpdk.org/api/series/2075/?format=api",
            "web_url": "http://patches.dpdk.org/project/dpdk/list/?series=2075",
            "date": "2018-10-24T16:02:58",
            "name": "introduce telemetry library",
            "version": 8,
            "mbox": "http://patches.dpdk.org/series/2075/mbox/"
        }
    ],
    "comments": "http://patches.dpdk.org/api/patches/47370/comments/",
    "check": "success",
    "checks": "http://patches.dpdk.org/api/patches/47370/checks/",
    "tags": {},
    "related": [],
    "headers": {
        "Return-Path": "<dev-bounces@dpdk.org>",
        "X-Original-To": "patchwork@dpdk.org",
        "Delivered-To": "patchwork@dpdk.org",
        "Received": [
            "from [92.243.14.124] (localhost [127.0.0.1])\n\tby dpdk.org (Postfix) with ESMTP id D42B41B1F5;\n\tWed, 24 Oct 2018 18:03:41 +0200 (CEST)",
            "from mga14.intel.com (mga14.intel.com [192.55.52.115])\n\tby dpdk.org (Postfix) with ESMTP id 0C20C1B150\n\tfor <dev@dpdk.org>; Wed, 24 Oct 2018 18:03:27 +0200 (CEST)",
            "from orsmga004.jf.intel.com ([10.7.209.38])\n\tby fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384;\n\t24 Oct 2018 09:03:26 -0700",
            "from silpixa00397517.ir.intel.com (HELO\n\tsilpixa00397517.ger.corp.intel.com) ([10.237.222.54])\n\tby orsmga004.jf.intel.com with ESMTP; 24 Oct 2018 09:03:21 -0700"
        ],
        "X-Amp-Result": "SKIPPED(no attachment in message)",
        "X-Amp-File-Uploaded": "False",
        "X-ExtLoop1": "1",
        "X-IronPort-AV": "E=Sophos;i=\"5.54,420,1534834800\"; d=\"scan'208\";a=\"244010353\"",
        "From": "Kevin Laatz <kevin.laatz@intel.com>",
        "To": "dev@dpdk.org",
        "Cc": "harry.van.haaren@intel.com, stephen@networkplumber.org,\n\tgaetan.rivet@6wind.com, shreyansh.jain@nxp.com, thomas@monjalon.net, \n\tmattias.ronnblom@ericsson.com, bruce.richardson@intel.com,\n\tCiara Power <ciara.power@intel.com>,\n\tBrian Archbold <brian.archbold@intel.com>,\n\tKevin Laatz <kevin.laatz@intel.com>",
        "Date": "Wed, 24 Oct 2018 17:03:06 +0100",
        "Message-Id": "<20181024160311.85457-9-kevin.laatz@intel.com>",
        "X-Mailer": "git-send-email 2.9.5",
        "In-Reply-To": "<20181024160311.85457-1-kevin.laatz@intel.com>",
        "References": "<20181024132725.5142-1-kevin.laatz@intel.com>\n\t<20181024160311.85457-1-kevin.laatz@intel.com>",
        "Subject": "[dpdk-dev] [PATCH v8 08/13] telemetry: format json response when\n\tsending stats",
        "X-BeenThere": "dev@dpdk.org",
        "X-Mailman-Version": "2.1.15",
        "Precedence": "list",
        "List-Id": "DPDK patches and discussions <dev.dpdk.org>",
        "List-Unsubscribe": "<https://mails.dpdk.org/options/dev>,\n\t<mailto:dev-request@dpdk.org?subject=unsubscribe>",
        "List-Archive": "<http://mails.dpdk.org/archives/dev/>",
        "List-Post": "<mailto:dev@dpdk.org>",
        "List-Help": "<mailto:dev-request@dpdk.org?subject=help>",
        "List-Subscribe": "<https://mails.dpdk.org/listinfo/dev>,\n\t<mailto:dev-request@dpdk.org?subject=subscribe>",
        "Errors-To": "dev-bounces@dpdk.org",
        "Sender": "\"dev\" <dev-bounces@dpdk.org>"
    },
    "content": "From: Ciara Power <ciara.power@intel.com>\n\nThis patch adds functionality to create a JSON message in\norder to send it to a client socket.\n\nWhen stats are requested by a client, they are retrieved from\nthe metrics library and encoded in JSON format.\n\nSigned-off-by: Ciara Power <ciara.power@intel.com>\nSigned-off-by: Brian Archbold <brian.archbold@intel.com>\nSigned-off-by: Kevin Laatz <kevin.laatz@intel.com>\nAcked-by: Harry van Haaren <harry.van.haaren@intel.com>\n---\n lib/librte_telemetry/rte_telemetry.c | 309 ++++++++++++++++++++++++++++++++++-\n 1 file changed, 307 insertions(+), 2 deletions(-)",
    "diff": "diff --git a/lib/librte_telemetry/rte_telemetry.c b/lib/librte_telemetry/rte_telemetry.c\nindex 7616e7c..36cd748 100644\n--- a/lib/librte_telemetry/rte_telemetry.c\n+++ b/lib/librte_telemetry/rte_telemetry.c\n@@ -190,7 +190,7 @@ rte_telemetry_send_error_response(struct telemetry_impl *telemetry,\n \t\treturn -EPERM;\n \t}\n \n-\tjson_buffer = json_dumps(root, JSON_INDENT(2));\n+\tjson_buffer = json_dumps(root, 0);\n \tjson_decref(root);\n \n \tret = rte_telemetry_write_to_socket(telemetry, json_buffer);\n@@ -202,6 +202,304 @@ rte_telemetry_send_error_response(struct telemetry_impl *telemetry,\n \treturn 0;\n }\n \n+static int\n+rte_telemetry_get_metrics(struct telemetry_impl *telemetry, uint32_t port_id,\n+\tstruct rte_metric_value *metrics, struct rte_metric_name *names,\n+\tint num_metrics)\n+{\n+\tint ret, num_values;\n+\n+\tif (num_metrics < 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Invalid metrics count\");\n+\t\tgoto einval_fail;\n+\t} else if (num_metrics == 0) {\n+\t\tTELEMETRY_LOG_ERR(\"No metrics to display (none have been registered)\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tif (metrics == NULL) {\n+\t\tTELEMETRY_LOG_ERR(\"Metrics must be initialised.\");\n+\t\tgoto einval_fail;\n+\t}\n+\n+\tif (names == NULL) {\n+\t\tTELEMETRY_LOG_ERR(\"Names must be initialised.\");\n+\t\tgoto einval_fail;\n+\t}\n+\n+\tret = rte_metrics_get_names(names, num_metrics);\n+\tif (ret < 0 || ret > num_metrics) {\n+\t\tTELEMETRY_LOG_ERR(\"Cannot get metrics names\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tnum_values = rte_metrics_get_values(port_id, NULL, 0);\n+\tret = rte_metrics_get_values(port_id, metrics, num_values);\n+\tif (ret < 0 || ret > num_values) {\n+\t\tTELEMETRY_LOG_ERR(\"Cannot get metrics values\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\treturn 0;\n+\n+eperm_fail:\n+\tret = rte_telemetry_send_error_response(telemetry, -EPERM);\n+\tif (ret < 0)\n+\t\tTELEMETRY_LOG_ERR(\"Could not send error\");\n+\treturn -1;\n+\n+einval_fail:\n+\tret = rte_telemetry_send_error_response(telemetry, -EINVAL);\n+\tif (ret < 0)\n+\t\tTELEMETRY_LOG_ERR(\"Could not send error\");\n+\treturn -1;\n+\n+}\n+\n+static int32_t\n+rte_telemetry_json_format_stat(struct telemetry_impl *telemetry, json_t *stats,\n+\tconst char *metric_name, uint64_t metric_value)\n+{\n+\tint ret;\n+\tjson_t *stat = json_object();\n+\n+\tif (stat == NULL) {\n+\t\tTELEMETRY_LOG_ERR(\"Could not create stat JSON object\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tret = json_object_set_new(stat, \"name\", json_string(metric_name));\n+\tif (ret < 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Stat Name field cannot be set\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tret = json_object_set_new(stat, \"value\", json_integer(metric_value));\n+\tif (ret < 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Stat Value field cannot be set\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tret = json_array_append_new(stats, stat);\n+\tif (ret < 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Stat cannot be added to stats json array\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\treturn 0;\n+\n+eperm_fail:\n+\tret = rte_telemetry_send_error_response(telemetry, -EPERM);\n+\tif (ret < 0)\n+\t\tTELEMETRY_LOG_ERR(\"Could not send error\");\n+\treturn -1;\n+\n+}\n+\n+static int32_t\n+rte_telemetry_json_format_port(struct telemetry_impl *telemetry,\n+\tuint32_t port_id, json_t *ports, uint32_t *metric_ids,\n+\tuint32_t num_metric_ids)\n+{\n+\tstruct rte_metric_value *metrics = 0;\n+\tstruct rte_metric_name *names = 0;\n+\tint num_metrics, ret, err_ret;\n+\tjson_t *port, *stats;\n+\tuint32_t i;\n+\n+\tnum_metrics = rte_metrics_get_names(NULL, 0);\n+\tif (num_metrics < 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Cannot get metrics count\");\n+\t\tgoto einval_fail;\n+\t} else if (num_metrics == 0) {\n+\t\tTELEMETRY_LOG_ERR(\"No metrics to display (none have been registered)\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tmetrics = malloc(sizeof(struct rte_metric_value) * num_metrics);\n+\tnames = malloc(sizeof(struct rte_metric_name) * num_metrics);\n+\tif (metrics == NULL || names == NULL) {\n+\t\tTELEMETRY_LOG_ERR(\"Cannot allocate memory\");\n+\t\tfree(metrics);\n+\t\tfree(names);\n+\n+\t\terr_ret = rte_telemetry_send_error_response(telemetry, -ENOMEM);\n+\t\tif (err_ret < 0)\n+\t\t\tTELEMETRY_LOG_ERR(\"Could not send error\");\n+\t\treturn -1;\n+\t}\n+\n+\tret  = rte_telemetry_get_metrics(telemetry, port_id, metrics, names,\n+\t\tnum_metrics);\n+\tif (ret < 0) {\n+\t\tfree(metrics);\n+\t\tfree(names);\n+\t\tTELEMETRY_LOG_ERR(\"rte_telemetry_get_metrics failed\");\n+\t\treturn -1;\n+\t}\n+\n+\tport = json_object();\n+\tstats = json_array();\n+\tif (port == NULL || stats == NULL) {\n+\t\tTELEMETRY_LOG_ERR(\"Could not create port/stats JSON objects\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tret = json_object_set_new(port, \"port\", json_integer(port_id));\n+\tif (ret < 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Port field cannot be set\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tfor (i = 0; i < num_metric_ids; i++) {\n+\t\tint metric_id = metric_ids[i];\n+\t\tint metric_index = -1;\n+\t\tint metric_name_key = -1;\n+\t\tint32_t j;\n+\t\tuint64_t metric_value;\n+\n+\t\tif (metric_id >= num_metrics) {\n+\t\t\tTELEMETRY_LOG_ERR(\"Metric_id: %d is not valid\",\n+\t\t\t\t\tmetric_id);\n+\t\t\tgoto einval_fail;\n+\t\t}\n+\n+\t\tfor (j = 0; j < num_metrics; j++) {\n+\t\t\tif (metrics[j].key == metric_id) {\n+\t\t\t\tmetric_name_key = metrics[j].key;\n+\t\t\t\tmetric_index = j;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\n+\t\tconst char *metric_name = names[metric_name_key].name;\n+\t\tmetric_value = metrics[metric_index].value;\n+\n+\t\tif (metric_name_key < 0 || metric_index < 0) {\n+\t\t\tTELEMETRY_LOG_ERR(\"Could not get metric name/index\");\n+\t\t\tgoto eperm_fail;\n+\t\t}\n+\n+\t\tret = rte_telemetry_json_format_stat(telemetry, stats,\n+\t\t\tmetric_name, metric_value);\n+\t\tif (ret < 0) {\n+\t\t\tTELEMETRY_LOG_ERR(\"Format stat with id: %u failed\",\n+\t\t\t\t\tmetric_id);\n+\t\t\tfree(metrics);\n+\t\t\tfree(names);\n+\t\t\treturn -1;\n+\t\t}\n+\t}\n+\n+\tif (json_array_size(stats) == 0)\n+\t\tret = json_object_set_new(port, \"stats\", json_null());\n+\telse\n+\t\tret = json_object_set_new(port, \"stats\", stats);\n+\n+\tif (ret < 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Stats object cannot be set\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tret = json_array_append_new(ports, port);\n+\tif (ret < 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Port object cannot be added to ports array\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tfree(metrics);\n+\tfree(names);\n+\treturn 0;\n+\n+eperm_fail:\n+\tfree(metrics);\n+\tfree(names);\n+\tret = rte_telemetry_send_error_response(telemetry, -EPERM);\n+\tif (ret < 0)\n+\t\tTELEMETRY_LOG_ERR(\"Could not send error\");\n+\treturn -1;\n+\n+einval_fail:\n+\tfree(metrics);\n+\tfree(names);\n+\tret = rte_telemetry_send_error_response(telemetry, -EINVAL);\n+\tif (ret < 0)\n+\t\tTELEMETRY_LOG_ERR(\"Could not send error\");\n+\treturn -1;\n+}\n+\n+static int32_t\n+rte_telemetry_encode_json_format(struct telemetry_impl *telemetry,\n+\tuint32_t *port_ids, uint32_t num_port_ids, uint32_t *metric_ids,\n+\tuint32_t num_metric_ids, char **json_buffer)\n+{\n+\tint ret;\n+\tjson_t *root, *ports;\n+\tuint32_t i;\n+\n+\tif (num_port_ids <= 0 || num_metric_ids <= 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Please provide port and metric ids to query\");\n+\t\tgoto einval_fail;\n+\t}\n+\n+\tports = json_array();\n+\tif (ports == NULL) {\n+\t\tTELEMETRY_LOG_ERR(\"Could not create ports JSON array\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tfor (i = 0; i < num_port_ids; i++) {\n+\t\tif (!rte_eth_dev_is_valid_port(port_ids[i])) {\n+\t\t\tTELEMETRY_LOG_ERR(\"Port: %d invalid\", port_ids[i]);\n+\t\t\tgoto einval_fail;\n+\t\t}\n+\t}\n+\n+\tfor (i = 0; i < num_port_ids; i++) {\n+\t\tret = rte_telemetry_json_format_port(telemetry, port_ids[i],\n+\t\t\tports, metric_ids, num_metric_ids);\n+\t\tif (ret < 0) {\n+\t\t\tTELEMETRY_LOG_ERR(\"Format port in JSON failed\");\n+\t\t\treturn -1;\n+\t\t}\n+\t}\n+\n+\troot = json_object();\n+\tif (root == NULL) {\n+\t\tTELEMETRY_LOG_ERR(\"Could not create root JSON object\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tret = json_object_set_new(root, \"status_code\",\n+\t\tjson_string(\"Status OK: 200\"));\n+\tif (ret < 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Status code field cannot be set\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tret = json_object_set_new(root, \"data\", ports);\n+\tif (ret < 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Data field cannot be set\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\t*json_buffer = json_dumps(root, JSON_INDENT(2));\n+\tjson_decref(root);\n+\treturn 0;\n+\n+eperm_fail:\n+\tret = rte_telemetry_send_error_response(telemetry, -EPERM);\n+\tif (ret < 0)\n+\t\tTELEMETRY_LOG_ERR(\"Could not send error\");\n+\treturn -1;\n+\n+einval_fail:\n+\tret = rte_telemetry_send_error_response(telemetry, -EINVAL);\n+\tif (ret < 0)\n+\t\tTELEMETRY_LOG_ERR(\"Could not send error\");\n+\treturn -1;\n+}\n+\n int32_t\n rte_telemetry_send_ports_stats_values(uint32_t *metric_ids, int num_metric_ids,\n \tuint32_t *port_ids, int num_port_ids, struct telemetry_impl *telemetry)\n@@ -241,13 +539,20 @@ rte_telemetry_send_ports_stats_values(uint32_t *metric_ids, int num_metric_ids,\n \t\t}\n \n \t\tret = rte_telemetry_update_metrics_ethdev(telemetry,\n-\t\t\tport_ids[i], telemetry->reg_index);\n+\t\t\t\tport_ids[i], telemetry->reg_index);\n \t\tif (ret < 0) {\n \t\t\tTELEMETRY_LOG_ERR(\"Failed to update ethdev metrics\");\n \t\t\treturn -1;\n \t\t}\n \t}\n \n+\tret = rte_telemetry_encode_json_format(telemetry, port_ids,\n+\t\tnum_port_ids, metric_ids, num_metric_ids, &json_buffer);\n+\tif (ret < 0) {\n+\t\tTELEMETRY_LOG_ERR(\"JSON encode function failed\");\n+\t\treturn -1;\n+\t}\n+\n \tret = rte_telemetry_write_to_socket(telemetry, json_buffer);\n \tif (ret < 0) {\n \t\tTELEMETRY_LOG_ERR(\"Could not write to socket\");\n",
    "prefixes": [
        "v8",
        "08/13"
    ]
}