edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching0 forksRust18.3 MiB7f487c1Tahinli3 months ago659 commits
| 1 | //! The hardware encoder, held in a **child process**. |
| 2 | //! |
| 3 | //! Every entry point of the plugin catches unwinds ([`crate::hw`]), and that |
| 4 | //! guarantee has a floor it cannot reach: Mesa's VA-API drivers run their own C |
| 5 | //! worker threads, and `radeonsi` calls libc `abort()` from |
| 6 | //! `amdgpu_ctx_set_sw_reset_status` when the video ring resets under it |
| 7 | //! (measured 2026-08-13, mesa 26.1.6, `vcn_unified_0` hung during an AV1 encode |
| 8 | //! submission). A foreign thread's `abort()` is not an unwind and no |
| 9 | //! `catch_unwind` in this address space can see it, so the editor died with the |
| 10 | //! driver and took the export -- and the session -- with it. |
| 11 | //! |
| 12 | //! So the driver is not in this address space any more. `HwEncoder` here is a |
| 13 | //! handle on a child running `hw-encode-child`, which is the one process that |
| 14 | //! `dlopen`s the plugin and therefore the only one a driver may kill. When it |
| 15 | //! dies -- by `abort()`, by any signal, or by going quiet past |
| 16 | //! [`Remote::deadline`] -- the parent sees a closed socket or a timed-out read, |
| 17 | //! reaps the child, and reports a [`Lost`]. The export then |
| 18 | //! runs again on the software encoder ([`crate::export::start`]), or refuses in |
| 19 | //! words where a person picked the GPU by name. |
| 20 | //! |
| 21 | //! Two things cross between the processes and neither is a picture copied down a |
| 22 | //! pipe. Pictures that live in memory are written into a shared anonymous file |
| 23 | //! (`memfd`, mapped in both), so a frame costs one `memcpy` and a 10-byte |
| 24 | //! message. Pictures still on the GPU never move at all: the DRM-PRIME |
| 25 | //! descriptors travel as file descriptors over `SCM_RIGHTS`, so the zero-copy |
| 26 | //! export path stays zero-copy across the process boundary. |
| 27 | |
| 28 | use c_void; |
| 29 | use Read; |
| 30 | use ; |
| 31 | use UnixStream; |
| 32 | use PathBuf; |
| 33 | use ; |
| 34 | use Duration; |
| 35 | |
| 36 | use crate; |
| 37 | use crateVhDma; |
| 38 | |
| 39 | /// The words every "the child is gone" error carries, for the person reading |
| 40 | /// the log; what *decides* anything is [`Lost`], never this text. |
| 41 | pub const HW_LOST: &str = "the hardware encoder process"; |
| 42 | |
| 43 | /// The error a dead child carries. |
| 44 | /// |
| 45 | /// A type and not a phrase. [`is_lost`] is what commits a whole export to being |
| 46 | /// written again from the first frame ([`crate::export::start`]), and matching |
| 47 | /// on words handed that decision to any future error whose message happened to |
| 48 | /// mention the child -- a silent re-encode of a feature film, caused by a |
| 49 | /// wording. Nothing outside this module can make one. |
| 50 | |
| 51 | ; |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | /// Whether an error is one: a driver that took its process down, rather than a |
| 62 | /// picture this engine got wrong. |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | /// The helper's file name; it is built into the same directory as the editor. |
| 68 | const BIN: &str = "hw-encode-child"; |
| 69 | |
| 70 | /// Which encoder the child opens. Sent as argv, so the child needs no protocol |
| 71 | /// before it has one. |
| 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 | /// Everything an open needs, in one place because it travels as argv and comes |
| 102 | /// back as a struct on the other side. |
| 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 | // Requests, parent to child. |
| 182 | pub const T_ENCODE: u8 = 1; |
| 183 | pub const T_DMA: u8 = 2; |
| 184 | pub const T_DRAIN: u8 = 3; |
| 185 | pub const T_GEOM: u8 = 4; |
| 186 | // Replies, child to parent. |
| 187 | pub const R_NONE: u8 = 0; |
| 188 | pub const R_AU: u8 = 1; |
| 189 | pub const R_ERR: u8 = 2; |
| 190 | |
| 191 | /// A `memfd` mapped into this process: the staging buffer one picture is written |
| 192 | /// into and read out of without either side owning the other's memory. |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | // The mapping is used from one thread at a time (the export worker), as the |
| 267 | // session it belongs to is. |
| 268 | unsafe |
| 269 | |
| 270 | /// Sends one message with its file descriptors attached. |
| 271 | /// |
| 272 | /// `MSG_NOSIGNAL` is the point of writing this by hand rather than |
| 273 | /// [`Write::write_all`]: a child that has just died leaves a socket whose next |
| 274 | /// write raises `SIGPIPE`, and a crash-containment path that kills the editor |
| 275 | /// with a signal of its own would be no containment at all. |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | /// Reads the one byte a message's descriptors ride on, and those descriptors. |
| 321 | /// |
| 322 | /// Ancillary data attaches to a position in the byte stream, so the tag is read |
| 323 | /// with `recvmsg` and everything after it with an ordinary read. |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | /// One access unit, or the child's own error, read off the socket. |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | /// Where the helper lives: told outright, or beside the running executable -- |
| 386 | /// which is `target/<profile>/` for the editor and one directory up from |
| 387 | /// `deps/` for a test binary. |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | /// Whether an isolated encoder can be had at all -- a helper to run. `false` |
| 400 | /// leaves the caller on the in-process seat, which is what a build that did not |
| 401 | /// produce the helper has always had. |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | /// The test-only stand-in seat: a child that opens no driver at all, so the |
| 407 | /// containment paths below can be exercised on a machine whose GPU is wedged -- |
| 408 | /// which is the only machine where they matter and the only one where the real |
| 409 | /// trigger cannot be pulled. |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | /// A hardware encode session living in another process. |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | /// How many answered calls it takes before the wait stops being the configured |
| 432 | /// ceiling and becomes this encoder's own measure, how much slower than its |
| 433 | /// slowest answer a call may be before it is a wedge, and the least it is ever |
| 434 | /// given. |
| 435 | /// |
| 436 | /// The ceiling has to be generous -- an encoder opening on a 4K frame is not a |
| 437 | /// dead one -- and the export spends every second of it on a child that will |
| 438 | /// never answer again: the file is owed the software encoder and the wait is |
| 439 | /// pure delay before it starts. The window is what shrinks, not the ceiling. |
| 440 | /// Once a child has answered [`CALIBRATE`] calls, the machine has said how long |
| 441 | /// its own frames take, and eight times the slowest of them (never under |
| 442 | /// [`FLOOR`]) contains a wedge in two or three seconds rather than fifteen, |
| 443 | /// while a slow open still gets the whole ceiling and is never killed for |
| 444 | /// being slow. The editor is live throughout either way -- the wait is on the |
| 445 | /// export's own thread and the card goes on repainting -- so what this buys is |
| 446 | /// the delay, not the window. |
| 447 | const CALIBRATE: u32 = 8; |
| 448 | const SLACK: u32 = 8; |
| 449 | const FLOOR: Duration = from_secs; |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | // As the in-process session: one thread at a time (the export worker). |
| 672 | unsafe |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 |