Skip to content

Commit

Permalink
feat: added bulk processing for pcie devices
Browse files Browse the repository at this point in the history
  • Loading branch information
zleyyij committed Feb 18, 2024
1 parent 1522514 commit 1ffa957
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,30 @@ Here's an example curl request:
```
curl "http://127.0.0.1:3000/api/pcie/?identifier=PCI%5CVEN_8086%26DEV_7A4D%26SUBSYS_00000000%26REV_11%5C3%2611583659%260%26A9%0A"
```

For bulk processing, you may submit a `POST` request to the same endpoint with a `Content-Type` of `application/json` and a payload containing an array of PCIe device identifier strings.

The endpoint will return an array of objects (same shape as the `GET` request), or if an identifier string was unable to be processed successfully, `null` will substitute in the response.

Here's an example curl request:
```
curl -X POST http://127.0.0.1:3000/api/pcie/ -H "Content-Type: application/json" -d '["PCI\\\\VEN_1022&DEV_43B4&SUBSYS_33
061B21&REV_02\\\\5&3B34128B&0&30020B","PCI\\\\VEN_1022&DEV_1444&SUBSYS_00000000&REV_00\\\\3&11583659&0&C4","PCI\\\\VEN_1022&DEV_43BC&SUBSYS_11421B21&REV_02\\\\4&2C18E2E3&0&000B","PCI\\\\
VEN_1022&DEV_43B4&SUBSYS_33061B21&REV_02\\\\5&3B34128B&0&28020B","PCI\\\\VEN_1022&DEV_1441&SUBSYS_00000000&REV_00\\\\3&11583659&0&C1","PCI\\\\VEN_1022&DEV_43B4&SUBSYS_33061B21&REV_02\\\\
5&3B34128B&0&38020B"]'
```
And here's example response (cropped):
```
[
{
"vendor":"Advanced Micro Devices, Inc. [AMD]",
"device":"300 Series Chipset PCIe Port",
"subsystem":null
},
{
"vendor":"Advanced Micro Devices, Inc. [AMD]",
"device":"Matisse/Vermeer Data Fabric: Device 18h; Function 4",
"subsystem":null
},
]
```
34 changes: 31 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ mod usb;

use axum::extract::Query;
use axum::http::{HeaderValue, StatusCode};
use axum::routing::post;
use axum::{extract::State, routing::get, Json, Router};
use chrono::Local;
use clap::Parser;
use colored::*;
use cpu::{Cpu, CpuCache};
use log::{error, info};
use log::{error, info, warn};
use log::{Level, LevelFilter, Metadata, Record};
use pcie::PcieCache;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -98,7 +99,7 @@ async fn get_usb_handler(
}

#[derive(Debug, Deserialize, Serialize)]
struct PcieQuery {
struct GetPcieQuery {
identifier: String,
}

Expand All @@ -113,7 +114,7 @@ struct PcieResponse {
/// It relies on a globally shared [AppState] to re-use the pcie cache
async fn get_pcie_handler(
State(state): State<AppState>,
Query(query): Query<PcieQuery>,
Query(query): Query<GetPcieQuery>,
) -> Result<Json<PcieResponse>, StatusCode> {
let results = state.pcie_cache.find(&query.identifier);
match results {
Expand All @@ -129,6 +130,32 @@ async fn get_pcie_handler(
}
}

/// This handler accepts a `POST` request to `/api/pcie/`, with a body containing a serialized array of strings.
/// It relies on a globally shared [AppState] to re-use the pcie cache, and is largely identical to [get_pcie_handler], but
/// is intended for batching
async fn post_pcie_handler(
State(state): State<AppState>,
Json(query): Json<Vec<String>>,
) -> Result<Json<Vec<Option<PcieResponse>>>, StatusCode> {
let mut response: Vec<Option<PcieResponse>> = Vec::with_capacity(16);
for entry in query {
match state.pcie_cache.find(&entry) {
Ok(r) => {
response.push(Some(PcieResponse {
vendor: r.0.map(|v| v.name),
device: r.1.map(|d| d.name),
subsystem: r.2.map(|s| s.name),
}))
},
Err(e) => {
warn!("post pcie handler error: when processing the device identifier {:?}, an error was returned: {:?}", entry, e);
response.push(None);
}
}
}
Ok(Json(response))
}

#[derive(Debug, Deserialize, Serialize)]
struct CpuQuery {
pub name: String,
Expand Down Expand Up @@ -164,6 +191,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.route("/api/cpus/", get(get_cpu_handler))
.route("/api/usbs/", get(get_usb_handler))
.route("/api/pcie/", get(get_pcie_handler))
.route("/api/pcie/", post(post_pcie_handler))
.layer(CorsLayer::new().allow_origin("*".parse::<HeaderValue>().unwrap()))
.with_state(AppState {
cpu_cache: CpuCache::new(),
Expand Down

0 comments on commit 1ffa957

Please sign in to comment.