get:
Show a patch.

patch:
Update a patch.

put:
Update a patch.

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

{
    "id": 43816,
    "url": "http://patches.dpdk.org/api/patches/43816/?format=api",
    "web_url": "http://patches.dpdk.org/project/dpdk/patch/1535026093-101872-7-git-send-email-ciara.power@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": "<1535026093-101872-7-git-send-email-ciara.power@intel.com>",
    "list_archive_url": "https://inbox.dpdk.org/dev/1535026093-101872-7-git-send-email-ciara.power@intel.com",
    "date": "2018-08-23T12:08:08",
    "name": "[06/11] telemetry: format json response when sending stats",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": true,
    "hash": "6ca8b840c04ebbba017cb2e18f6f0bde2bf8b85e",
    "submitter": {
        "id": 978,
        "url": "http://patches.dpdk.org/api/people/978/?format=api",
        "name": "Ciara Power",
        "email": "ciara.power@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/1535026093-101872-7-git-send-email-ciara.power@intel.com/mbox/",
    "series": [
        {
            "id": 1038,
            "url": "http://patches.dpdk.org/api/series/1038/?format=api",
            "web_url": "http://patches.dpdk.org/project/dpdk/list/?series=1038",
            "date": "2018-08-23T12:08:02",
            "name": "introduce telemetry library",
            "version": 1,
            "mbox": "http://patches.dpdk.org/series/1038/mbox/"
        }
    ],
    "comments": "http://patches.dpdk.org/api/patches/43816/comments/",
    "check": "fail",
    "checks": "http://patches.dpdk.org/api/patches/43816/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 2FE99532C;\n\tThu, 23 Aug 2018 14:08:40 +0200 (CEST)",
            "from mga06.intel.com (mga06.intel.com [134.134.136.31])\n\tby dpdk.org (Postfix) with ESMTP id 72D852C4F\n\tfor <dev@dpdk.org>; Thu, 23 Aug 2018 14:08:33 +0200 (CEST)",
            "from fmsmga005.fm.intel.com ([10.253.24.32])\n\tby orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384;\n\t23 Aug 2018 05:08:33 -0700",
            "from silpixa00399503.ir.intel.com ([10.237.222.102])\n\tby fmsmga005.fm.intel.com with ESMTP; 23 Aug 2018 05:08:31 -0700"
        ],
        "X-Amp-Result": "SKIPPED(no attachment in message)",
        "X-Amp-File-Uploaded": "False",
        "X-ExtLoop1": "1",
        "X-IronPort-AV": "E=Sophos;i=\"5.53,278,1531810800\"; d=\"scan'208\";a=\"256613860\"",
        "From": "Ciara Power <ciara.power@intel.com>",
        "To": "harry.van.haaren@intel.com, brian.archbold@intel.com,\n\temma.kenny@intel.com, ciara.power@intel.com",
        "Cc": "dev@dpdk.org",
        "Date": "Thu, 23 Aug 2018 13:08:08 +0100",
        "Message-Id": "<1535026093-101872-7-git-send-email-ciara.power@intel.com>",
        "X-Mailer": "git-send-email 2.7.4",
        "In-Reply-To": "<1535026093-101872-1-git-send-email-ciara.power@intel.com>",
        "References": "<1535026093-101872-1-git-send-email-ciara.power@intel.com>",
        "Subject": "[dpdk-dev] [PATCH 06/11] 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": "This 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>\n---\n lib/librte_telemetry/rte_telemetry.c | 314 ++++++++++++++++++++++++++++++++++-\n 1 file changed, 313 insertions(+), 1 deletion(-)",
    "diff": "diff --git a/lib/librte_telemetry/rte_telemetry.c b/lib/librte_telemetry/rte_telemetry.c\nindex bd09374..ba04d3d 100644\n--- a/lib/librte_telemetry/rte_telemetry.c\n+++ b/lib/librte_telemetry/rte_telemetry.c\n@@ -179,7 +179,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@@ -190,6 +190,311 @@ 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(\"Error - Invalid metrics count\\n\");\n+\t\tgoto einval_fail;\n+\t} else if (num_metrics == 0) {\n+\t\tTELEMETRY_LOG_ERR(\"No metrics to display (none have been \"\n+\t\t\t\"registered)\\n\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tif (metrics == NULL) {\n+\t\tTELEMETRY_LOG_ERR(\"Error - metrics must be initialised.\\n\");\n+\t\tgoto einval_fail;\n+\t}\n+\n+\tif (names == NULL) {\n+\t\tTELEMETRY_LOG_ERR(\"Error - names must be initialised.\\n\");\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(\"Error - Cannot get metrics names\\n\");\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(\"Error - Cannot get metrics values\\n\");\n+\t\tgoto eperm_fail;\n+\t}\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(\"Error - Could not send error\\n\");\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(\"Error - Could not send error\\n\");\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) {\n+\t\tTELEMETRY_LOG_ERR(\"Error - Could not create stat JSON \"\n+\t\t\t\"object\\n\");\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(\"Error - Stat Name field cannot be set\\n\");\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(\"Error - Stat Value field cannot be set\\n\");\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(\"Error - Stat cannot be added to stats json \"\n+\t\t\t\"array\\n\");\n+\t\tgoto eperm_fail;\n+\t}\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(\"Error - Could not send error\\n\");\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;\n+\n+\tnum_metrics = rte_metrics_get_names(NULL, 0);\n+\tif (num_metrics < 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Error - Cannot get metrics count\\n\");\n+\t\tgoto einval_fail;\n+\t} else if (num_metrics == 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Error - No metrics to display (none have \"\n+\t\t\t\"been registered)\\n\");\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(\"Error - Cannot allocate memory\\n\");\n+\t\tfree(metrics);\n+\t\tfree(names);\n+\t\tint err_ret = rte_telemetry_send_error_response(telemetry,\n+\t\t\t-ENOMEM);\n+\t\tif (err_ret < 0)\n+\t\t\tTELEMETRY_LOG_ERR(\"Error - Could not send error\\n\");\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(\"Error - rte_telemetry_get_metrics failed\\n\");\n+\t\treturn -1;\n+\t}\n+\n+\tjson_t *port = json_object();\n+\tjson_t *stats = json_array();\n+\tif (!port || !stats) {\n+\t\tTELEMETRY_LOG_ERR(\"Error - Could not create port/stats JSON \"\n+\t\t\t\"objects\\n\");\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(\"Error - Port field cannot be set\\n\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tuint32_t i;\n+\tfor (i = 0; i < num_metric_ids; i++) {\n+\t\tif (metric_ids[i] >= (uint32_t)num_metrics) {\n+\t\t\tTELEMETRY_LOG_ERR(\"Error - Metric_id: %d is \"\n+\t\t\t\t\"not valid\\n\", metric_ids[i]);\n+\t\t\tgoto einval_fail;\n+\t\t}\n+\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+\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\tif (metric_name_key < 0 || metric_index < 0) {\n+\t\t\tTELEMETRY_LOG_ERR(\"Error - Could not get metric \"\n+\t\t\t\t\"name/index\\n\");\n+\t\t\tgoto eperm_fail;\n+\t\t}\n+\n+\t\tconst char *metric_name = names[metric_name_key].name;\n+\t\tuint64_t metric_value = metrics[metric_index].value;\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(\"Error - Format stat with id: %u \"\n+\t\t\t\t\"failed\\n\", metric_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(\"Error - Stats object cannot be set\\n\");\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(\"Error - Port object cannot be added to \"\n+\t\t\t\"ports array\\n\");\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(\"Error - Could not send error\\n\");\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(\"Error - Could not send error\\n\");\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+\n+\tif (num_port_ids <= 0 || num_metric_ids <= 0) {\n+\t\tTELEMETRY_LOG_ERR(\"Error - Please provide port and metric ids \"\n+\t\t\t\"to query\\n\");\n+\t\tgoto einval_fail;\n+\t}\n+\n+\tports = json_array();\n+\tif (!ports) {\n+\t\tTELEMETRY_LOG_ERR(\"Error - Could not create ports JSON \"\n+\t\t\t\"array\\n\");\n+\t\tgoto eperm_fail;\n+\t}\n+\n+\tuint32_t i;\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(\"Error - Port: %d invalid\\n\",\n+\t\t\t\tport_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(\"Error - format port in JSON \"\n+\t\t\t\t\"failed\\n\");\n+\t\t\treturn -1;\n+\t\t}\n+\t}\n+\n+\troot = json_object();\n+\tif (!root) {\n+\t\tTELEMETRY_LOG_ERR(\"Error - Could not create root JSON \"\n+\t\t\t\"object\\n\");\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(\"Error - Status code field cannot be set\\n\");\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(\"Error - Data field cannot be set\\n\");\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(\"Error - Could not send error\\n\");\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(\"Error - Could not send error\\n\");\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@@ -235,6 +540,13 @@ rte_telemetry_send_ports_stats_values(uint32_t *metric_ids, int num_metric_ids,\n \t}\n \n \tchar *json_buffer = NULL;\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(\"Error - JSON encode function failed\\n\");\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(\"Error - Could not write to socket\\n\");\n",
    "prefixes": [
        "06/11"
    ]
}