summaryrefslogtreecommitdiffstats
path: root/src/plugins/perfmon/intel/bundle/iio_bw.c
blob: 9aaca42233f1d06d665d2e0410194f1c67636089 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight
/*
 * Copyright (c) 2021 Intel and/or its affiliates.
 * 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.
 */

#include <perfmon/perfmon.h>
#include <perfmon/intel/uncore.h>
#include <vlib/pci/pci.h>
#include <vppinfra/format.h>
#include <linux/limits.h>
#include <fcntl.h>
#include <math.h>

typedef struct
{
  u8 socket_id;
  u8 sad_id;
  u8 iio_unit_id;
} iio_uncore_sad_t;
typedef u32 index_t;

static const char *procfs_pci_path = "/proc/bus/pci";

#define PCM_INTEL_PCI_VENDOR_ID	       0x8086
#define SNR_ICX_SAD_CONTROL_CFG_OFFSET 0x3F4
#define SNR_ICX_MESH2IIO_MMAP_DID      0x09A2

static const u8 icx_sad_to_pmu_id_mapping[] = { 5, 0, 1, 2, 3, 4 };

static const char *iio_bw_footer_message =
  "* this bundle currently only measures x8 and x16 PCIe devices on Port #0\n"
  "or Port #2. Please see the \"Intel® Xeon® Processor Scalable Memory\n"
  "Family Uncore Performance Monitoring Reference Manual(336274)\"\n"
  "Section 2.4 for more information.";

static u32
get_sad_ctrl_cfg (vlib_pci_addr_t *addr)
{
  int fd = 0;
  u32 value;
  u8 *dev_node_name = format (0, "%s/%02x/%02x.%x", procfs_pci_path, addr->bus,
			      addr->slot, addr->function);

  fd = open ((char *) dev_node_name, O_RDWR);
  if (fd < 0)
    return -1;

  if (pread (fd, &value, sizeof (u32), SNR_ICX_SAD_CONTROL_CFG_OFFSET) <
      sizeof (u32))
    value = -1;

  close (fd);

  return value;
}

static u64
get_bus_to_sad_mappings (vlib_main_t *vm, index_t **ph, iio_uncore_sad_t **pp)
{
  index_t *h = 0;
  iio_uncore_sad_t *p = 0, *e = 0;
  vlib_pci_addr_t *addr = 0, *addrs;

  addrs = vlib_pci_get_all_dev_addrs ();

  vec_foreach (addr, addrs)
    {
      vlib_pci_device_info_t *d;
      d = vlib_pci_get_device_info (vm, addr, 0);

      if (!d)
	continue;

      if (d->vendor_id == PCM_INTEL_PCI_VENDOR_ID &&
	  d->device_id == SNR_ICX_MESH2IIO_MMAP_DID)
	{

	  u32 sad_ctrl_cfg = get_sad_ctrl_cfg (addr);
	  if (sad_ctrl_cfg == 0xFFFFFFFF)
	    {
	      vlib_pci_free_device_info (d);
	      continue;
	    }

	  pool_get_zero (p, e);

	  e->socket_id = (sad_ctrl_cfg & 0xf);
	  e->sad_id = (sad_ctrl_cfg >> 4) & 0x7;
	  e->iio_unit_id = icx_sad_to_pmu_id_mapping[e->sad_id];

	  hash_set (h, addr->bus, e - p);
	}

      vlib_pci_free_device_info (d);
    }

  vec_free (addrs);

  *ph = h;
  *pp = p;

  return 0;
}

u8 *
format_stack_socket (u8 *s, va_list *va)
{
  iio_uncore_sad_t *e, *p = va_arg (*va, iio_uncore_sad_t *);
  index_t *h = va_arg (*va, index_t *);
  vlib_pci_addr_t root_bus, *addr = va_arg (*va, vlib_pci_addr_t *);
  clib_error_t *err = vlib_pci_get_device_root_bus (addr, &root_bus);
  if (err)
    {
      clib_error_free (err);
      return s;
    }

  uword *pu = hash_get (h, root_bus.bus);
  e = pool_elt_at_index (p, (index_t) pu[0]);

  s = format (s, "IIO%u/%u", e->socket_id, e->iio_unit_id);
  return s;
}

static clib_error_t *
init_intel_uncore_iio_bw (vlib_main_t *vm, struct perfmon_bundle *b)
{
  index_t *h = 0;
  iio_uncore_sad_t *p = 0;
  vlib_pci_addr_t *addr = 0, *addrs;
  u8 *s = 0;

  get_bus_to_sad_mappings (vm, &h, &p);

  s = format (0, "%-10s%-5s%-13s%-12s%-14s%-16s%s\n", "Stack", "Port",
	      "Address", "VID:PID", "Link Speed", "Driver", "Product Name");

  addrs = vlib_pci_get_all_dev_addrs ();

  vec_foreach (addr, addrs)
    {
      vlib_pci_device_info_t *d;
      d = vlib_pci_get_device_info (vm, addr, 0);

      if (!d)
	continue;

      if (d->device_class != PCI_CLASS_NETWORK_ETHERNET)
	continue;

      s = format (
	s, "%-10U%-5U%-13U%04x:%04x   %-14U%-16s%v\n", format_stack_socket, p,
	h, addr, format_vlib_pci_link_port, d, format_vlib_pci_addr, addr,
	d->vendor_id, d->device_id, format_vlib_pci_link_speed, d,
	d->driver_name ? (char *) d->driver_name : "", d->product_name);

      vlib_pci_free_device_info (d);
    }

  b->footer = (char *) format (s, "\n%s", iio_bw_footer_message);

  vec_free (addrs);
  pool_free (p);
  hash_free (h);

  return 0;
}

static u8 *
format_intel_uncore_iio_bw (u8 *s, va_list *args)
{
  perfmon_reading_t *r = va_arg (*args, perfmon_reading_t *);
  int col = va_arg (*args, int);
  f64 tr = r->time_running * 1e-9;
  f64 value = 0;

  switch (col)
    {
    case 0:
      s = format (s, "%9.2f", tr);
      break;
    default:
      if (r->time_running)
	{
	  value = r->value[col - 1] * 4 / tr;

	  if (value > 1.0e6)
	    s = format (s, "%9.0fM", value * 1e-6);
	  else if (value > 1.0e3)
	    s = format (s, "%9.0fK", value * 1e-3);
	  else
	    s = format (s, "%9.0f ", value);
	}

      break;
    }

  return s;
}

/*
 * This bundle is currently only supported and tested on Intel Icelake.
 */
static int
is_icelake ()
{
  return clib_cpu_supports_avx512_bitalg () && !clib_cpu_supports_movdir64b ();
}

static perfmon_cpu_supports_t iio_bw_cpu_supports[] = {
  { is_icelake, PERFMON_BUNDLE_TYPE_SYSTEM }
};

PERFMON_REGISTER_BUNDLE (intel_uncore_iio_bw_pci) = {
  .name = "iio-bandwidth-pci",
  .description = "pci iio memory reads and writes per iio stack *",
  .source = "intel-uncore",
  .events[0] = INTEL_UNCORE_E_IIO_UNC_IIO_DATA_REQ_OF_CPU_PART0_RD,
  .events[1] = INTEL_UNCORE_E_IIO_UNC_IIO_DATA_REQ_BY_CPU_PART0_WR,
  .events[2] = INTEL_UNCORE_E_IIO_UNC_IIO_DATA_REQ_BY_CPU_PART2_RD,
  .events[3] = INTEL_UNCORE_E_IIO_UNC_IIO_DATA_REQ_BY_CPU_PART2_WR,
  .n_events = 4,
  .cpu_supports = iio_bw_cpu_supports,
  .n_cpu_supports = ARRAY_LEN (iio_bw_cpu_supports),
  .format_fn = format_intel_uncore_iio_bw,
  .init_fn = init_intel_uncore_iio_bw,
  .column_headers = PERFMON_STRINGS ("RunTime", "PCIe Rd/P0", "PCIe Wr/P0",
				     "PCIe Rd/P2", "PCIe Wr/P2")
};

PERFMON_REGISTER_BUNDLE (intel_uncore_iio_bw_cpu) = {
  .name = "iio-bandwidth-cpu",
  .description = "cpu iio memory reads and writes per iio stack *",
  .source = "intel-uncore",
  .events[0] = INTEL_UNCORE_E_IIO_UNC_IIO_DATA_REQ_BY_CPU_PART0_RD,
  .events[1] = INTEL_UNCORE_E_IIO_UNC_IIO_DATA_REQ_BY_CPU_PART0_WR,
  .events[2] = INTEL_UNCORE_E_IIO_UNC_IIO_DATA_REQ_BY_CPU_PART2_RD,
  .events[3] = INTEL_UNCORE_E_IIO_UNC_IIO_DATA_REQ_BY_CPU_PART2_WR,
  .n_events = 4,
  .cpu_supports = iio_bw_cpu_supports,
  .n_cpu_supports = ARRAY_LEN (iio_bw_cpu_supports),
  .format_fn = format_intel_uncore_iio_bw,
  .init_fn = init_intel_uncore_iio_bw,
  .column_headers = PERFMON_STRINGS ("RunTime", "CPU Rd/P0", "CPU Wr/P0",
				     "CPU Rd/P2", "CPU Wr/P2")
};