Skip to contentSkip to conversation

PCI: minor performance optimization

PATCH In review Cao jin 2 participants 7 messages Nov 20, 2015 – Nov 21, 2015

lore
Share & tools
lore

Cao jin

author
#0 lore raw

PATCH 1/1 PCI: minor performance optimization

1. Do param check in pci_add_capability2(), as it is a public API.
2. As spec says, each capability must be DWORD aligned, so an optimization can be done via Loop Unrolling.

---
 hw/pci/pci.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
hw/pci/pci.c +8 -4
index 168b9cc..1e99603 100644
@@ -1924,13 +1924,15 @@ PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
 static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
 {
     int offset = PCI_CONFIG_HEADER_SIZE;
-    int i;
-    for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
+    int i = PCI_CONFIG_HEADER_SIZE;;
+
+    for (; i < PCI_CONFIG_SPACE_SIZE; i = i + 4) {
         if (pdev->used[i])
-            offset = i + 1;
-        else if (i - offset + 1 == size)
+            offset = i + 4;
+        else if (i - offset >= size)
             return offset;
     }
+
     return 0;
 }
 
@@ -2144,6 +2146,8 @@ int pci_add_capability2(PCIDevice *pdev, uint8_t cap_id,
     uint8_t *config;
     int i, overlapping_cap;
 
+    assert(size > 0);
+
     if (!offset) {
         offset = pci_find_space(pdev, size);
         if (!offset) {
Signature · 1 line
2.1.0
#1 lore raw
Quoted 1 line · Cao jin

1. Do param check in pci_add_capability2(), as it is a public API.

Separate patch pls.

Quoted 2 lines

2. As spec says, each capability must be DWORD aligned, so an optimization can be done via Loop Unrolling.

Why do we want to optimize it?

Quoted 41 lines
---
 hw/pci/pci.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
hw/pci/pci.c +8 -4
index 168b9cc..1e99603 100644
@@ -1924,13 +1924,15 @@ PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
 static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
 {
     int offset = PCI_CONFIG_HEADER_SIZE;
-    int i;
-    for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
+    int i = PCI_CONFIG_HEADER_SIZE;;
+
+    for (; i < PCI_CONFIG_SPACE_SIZE; i = i + 4) {
         if (pdev->used[i])
-            offset = i + 1;
-        else if (i - offset + 1 == size)
+            offset = i + 4;
+        else if (i - offset >= size)
             return offset;
     }
+
     return 0;
 }
 
@@ -2144,6 +2146,8 @@ int pci_add_capability2(PCIDevice *pdev, uint8_t cap_id,
     uint8_t *config;
     int i, overlapping_cap;
 
+    assert(size > 0);
+
     if (!offset) {
         offset = pci_find_space(pdev, size);
         if (!offset) {
Signature · 1 line
2.1.0
author
#2 lore raw
Quoted 4 lines · Michael S. Tsirkin
Quoted 1 line · Cao jin

1. Do param check in pci_add_capability2(), as it is a public API.

Separate patch pls.

OK

Quoted 6 lines
Quoted 3 lines

2. As spec says, each capability must be DWORD aligned, so an optimization can be done via Loop Unrolling.

Why do we want to optimize it?

For tiny performance improvement via less loop. take pcie express capability(60 bytes at most) for example, it may loop 60 times, now we just need 15 times, a quarter of before.

Quoted 43 lines
Quoted 40 lines
---
  hw/pci/pci.c | 12 ++++++++----
  1 file changed, 8 insertions(+), 4 deletions(-)
hw/pci/pci.c +8 -5
index 168b9cc..1e99603 100644
@@ -1924,13 +1924,15 @@ PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
  static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
  {
      int offset = PCI_CONFIG_HEADER_SIZE;
-    int i;
-    for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
+    int i = PCI_CONFIG_HEADER_SIZE;;
+
+    for (; i < PCI_CONFIG_SPACE_SIZE; i = i + 4) {
          if (pdev->used[i])
-            offset = i + 1;
-        else if (i - offset + 1 == size)
+            offset = i + 4;
+        else if (i - offset >= size)
              return offset;
      }
+
      return 0;
  }
 
@@ -2144,6 +2146,8 @@ int pci_add_capability2(PCIDevice *pdev, uint8_t cap_id,
      uint8_t *config;
      int i, overlapping_cap;
 
+    assert(size > 0);
+
      if (!offset) {
          offset = pci_find_space(pdev, size);
          if (!offset) {
--
Quoted 1 line

2.1.0

.

Signature · 3 lines
Yours Sincerely,

Cao Jin
#3 lore raw
Quoted 14 lines · Cao jin
Quoted 4 lines · Michael S. Tsirkin
Quoted 1 line · Cao jin

1. Do param check in pci_add_capability2(), as it is a public API.

Separate patch pls.

OK

Quoted 4 lines
Quoted 3 lines

2. As spec says, each capability must be DWORD aligned, so an optimization can be done via Loop Unrolling.

Quoted 6 lines
Quoted 2 lines

Why do we want to optimize it?

For tiny performance improvement via less loop. take pcie express capability(60 bytes at most) for example, it may loop 60 times, now we just need 15 times, a quarter of before.

But who cares? This is not a data path operation.

Quoted 48 lines
Quoted 43 lines
Quoted 40 lines
---
 hw/pci/pci.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
hw/pci/pci.c +8 -5
index 168b9cc..1e99603 100644
@@ -1924,13 +1924,15 @@ PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
 static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
 {
     int offset = PCI_CONFIG_HEADER_SIZE;
-    int i;
-    for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
+    int i = PCI_CONFIG_HEADER_SIZE;;
+
+    for (; i < PCI_CONFIG_SPACE_SIZE; i = i + 4) {
         if (pdev->used[i])
-            offset = i + 1;
-        else if (i - offset + 1 == size)
+            offset = i + 4;
+        else if (i - offset >= size)
             return offset;
     }
+
     return 0;
 }
 
@@ -2144,6 +2146,8 @@ int pci_add_capability2(PCIDevice *pdev, uint8_t cap_id,
     uint8_t *config;
     int i, overlapping_cap;
 
+    assert(size > 0);
+
     if (!offset) {
         offset = pci_find_space(pdev, size);
         if (!offset) {
--
Quoted 1 line

2.1.0

.

Signature · 3 lines
Yours Sincerely,

Cao Jin
author
#4 lore raw
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Cao jin
Quoted 5 lines · Michael S. Tsirkin
Quoted 2 lines · Cao jin

2. As spec says, each capability must be DWORD aligned, so an optimization can be done via Loop Unrolling.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Why do we want to optimize it?

For tiny performance improvement via less loop. take pcie express capability(60 bytes at most) for example, it may loop 60 times, now we just need 15 times, a quarter of before.

But who cares? This is not a data path operation.

It is tiny thing I found when browsing code. When found there are several places looks like this, I think maybe it does good to qemu to do this and CCed to you because it don`t look like a simple trivial patch.

So, hey Michael, if you dont like this kind of optimization, thatt ok, forget it. But I think it make me little confused when determine which kind of patch should be CCed to you.

Quoted 51 lines
Quoted 45 lines
Quoted 35 lines
Quoted 35 lines
---
  hw/pci/pci.c | 12 ++++++++----
  1 file changed, 8 insertions(+), 4 deletions(-)
hw/pci/pci.c +6 -4
index 168b9cc..1e99603 100644
@@ -1924,13 +1924,15 @@ PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
  static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
  {
      int offset = PCI_CONFIG_HEADER_SIZE;
-    int i;
-    for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
+    int i = PCI_CONFIG_HEADER_SIZE;;
+
+    for (; i < PCI_CONFIG_SPACE_SIZE; i = i + 4) {
          if (pdev->used[i])
-            offset = i + 1;
-        else if (i - offset + 1 == size)
+            offset = i + 4;
+        else if (i - offset >= size)
              return offset;
      }
+
      return 0;
  }
 
@@ -2144,6 +2146,8 @@ int pci_add_capability2(PCIDevice *pdev, uint8_t cap_id,
      uint8_t *config;
      int i, overlapping_cap;
 
Quoted 9 lines
Quoted 7 lines
hw/pci/pci.c +2 -1
+    assert(size > 0);
+
      if (!offset) {
          offset = pci_find_space(pdev, size);
          if (!offset) {
--

2.1.0

.

Quoted 4 lines

--
Yours Sincerely,

Cao Jin

.

Signature · 3 lines
Yours Sincerely,

Cao Jin
#5 lore raw
Quoted 21 lines · Cao jin
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Cao jin
Quoted 5 lines · Michael S. Tsirkin
Quoted 2 lines · Cao jin

2. As spec says, each capability must be DWORD aligned, so an optimization can be done via Loop Unrolling.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Why do we want to optimize it?

For tiny performance improvement via less loop. take pcie express capability(60 bytes at most) for example, it may loop 60 times, now we just need 15 times, a quarter of before.

But who cares? This is not a data path operation.

Quoted 7 lines

It is tiny thing I found when browsing code. When found there are several places looks like this, I think maybe it does good to qemu to do this and CCed to you because it don`t look like a simple trivial patch.

So, hey Michael, if you dont like this kind of optimization, thatt ok, forget it. But I think it make me little confused when determine which kind of patch should be CCed to you.

Optimization patches should normally include performance numbers if they are to be merged.
Try to come up with a benchmark and you will realize that the speed of this function has no effect under even half way realistic conditions.

Quoted 56 lines
Quoted 51 lines
Quoted 45 lines
Quoted 35 lines
Quoted 35 lines
---
 hw/pci/pci.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
hw/pci/pci.c +6 -4
index 168b9cc..1e99603 100644
@@ -1924,13 +1924,15 @@ PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
 static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
 {
     int offset = PCI_CONFIG_HEADER_SIZE;
-    int i;
-    for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
+    int i = PCI_CONFIG_HEADER_SIZE;;
+
+    for (; i < PCI_CONFIG_SPACE_SIZE; i = i + 4) {
         if (pdev->used[i])
-            offset = i + 1;
-        else if (i - offset + 1 == size)
+            offset = i + 4;
+        else if (i - offset >= size)
             return offset;
     }
+
     return 0;
 }
 
@@ -2144,6 +2146,8 @@ int pci_add_capability2(PCIDevice *pdev, uint8_t cap_id,
     uint8_t *config;
     int i, overlapping_cap;
 
Quoted 9 lines
Quoted 7 lines
hw/pci/pci.c +2 -1
+    assert(size > 0);
+
     if (!offset) {
         offset = pci_find_space(pdev, size);
         if (!offset) {
--

2.1.0

.

Quoted 4 lines

--
Yours Sincerely,

Cao Jin

.

Signature · 3 lines
Yours Sincerely,

Cao Jin
author
#6 lore raw
Quoted 26 lines · Michael S. Tsirkin
Quoted 25 lines · Cao jin
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Cao jin
Quoted 5 lines · Michael S. Tsirkin
Quoted 2 lines · Cao jin

2. As spec says, each capability must be DWORD aligned, so an optimization can be done via Loop Unrolling.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Why do we want to optimize it?

For tiny performance improvement via less loop. take pcie express capability(60 bytes at most) for example, it may loop 60 times, now we just need 15 times, a quarter of before.

But who cares? This is not a data path operation.

It is tiny thing I found when browsing code. When found there are several places looks like this, I think maybe it does good to qemu to do this and CCed to you because it don`t look like a simple trivial patch.

Quoted 9 lines
Quoted 3 lines

So, hey Michael, if you dont like this kind of optimization, thatt ok, forget it. But I think it make me little confused when determine which kind of patch should be CCed to you.

Optimization patches should normally include performance numbers if they are to be merged.
Try to come up with a benchmark and you will realize that the speed of this function has no effect under even half way realistic conditions.

Maybe you are right. OK, will send the param check patch to the qemu-trivial

Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

.

--
Yours Sincerely,

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Cao Jin

.

--
Yours Sincerely,

Cao Jin

.

Signature · 3 lines
Yours Sincerely,

Cao Jin