455 }
456
457 @Override
458 public boolean equals(Object obj) {
459 if (this == obj) {
460 return true;
461 }
462 if (obj instanceof ProcessHandleImpl) {
463 ProcessHandleImpl other = (ProcessHandleImpl) obj;
464 return (pid == other.pid) &&
465 (startTime == other.startTime
466 || startTime == 0
467 || other.startTime == 0);
468 }
469 return false;
470 }
471
472 /**
473 * Implementation of ProcessHandle.Info.
474 * Information snapshot about a process.
475 * The attributes of a process vary by operating system and not available
476 * in all implementations. Additionally, information about other processes
477 * is limited by the operating system privileges of the process making the request.
478 * If a value is not available, either a {@code null} or {@code -1} is stored.
479 * The accessor methods return {@code null} if the value is not available.
480 */
481 static class Info implements ProcessHandle.Info {
482 static {
483 initIDs();
484 }
485
486 /**
487 * Initialization of JNI fieldIDs.
488 */
489 private static native void initIDs();
490
491 /**
492 * Fill in this Info instance with information about the native process.
493 * If values are not available the native code does not modify the field.
494 * @param pid of the native process
495 */
496 private native void info0(long pid);
497
498 String command;
499 String[] arguments;
500 long startTime;
501 long totalTime;
502 String user;
503
504 Info() {
505 command = null;
506 arguments = null;
507 startTime = -1L;
508 totalTime = -1L;
509 user = null;
510 }
511
512 /**
513 * Returns the Info object with the fields from the process.
514 * Whatever fields are provided by native are returned.
515 * If the startTime of the process does not match the provided
516 * startTime then an empty Info is returned.
517 *
518 * @param pid the native process identifier
519 * @param startTime the startTime of the process being queried
520 * @return ProcessHandle.Info non-null; individual fields may be null
521 * or -1 if not available.
522 */
523 public static ProcessHandle.Info info(long pid, long startTime) {
524 Info info = new Info();
525 info.info0(pid);
526 if (startTime != info.startTime) {
527 info.command = null;
528 info.arguments = null;
529 info.startTime = -1L;
530 info.totalTime = -1L;
531 info.user = null;
532 }
533 return info;
534 }
535
536 @Override
537 public Optional<String> command() {
538 return Optional.ofNullable(command);
539 }
540
541 @Override
542 public Optional<String[]> arguments() {
543 return Optional.ofNullable(arguments);
544 }
545
546 @Override
547 public Optional<Instant> startInstant() {
548 return (startTime > 0)
549 ? Optional.of(Instant.ofEpochMilli(startTime))
550 : Optional.empty();
551 }
552
553 @Override
554 public Optional<Duration> totalCpuDuration() {
555 return (totalTime != -1)
556 ? Optional.of(Duration.ofNanos(totalTime))
557 : Optional.empty();
558 }
559
560 @Override
561 public Optional<String> user() {
562 return Optional.ofNullable(user);
563 }
564
565 @Override
566 public String toString() {
567 StringBuilder sb = new StringBuilder(60);
568 sb.append('[');
569 if (user != null) {
570 sb.append("user: ");
571 sb.append(user());
572 }
573 if (command != null) {
574 if (sb.length() != 0) sb.append(", ");
575 sb.append("cmd: ");
576 sb.append(command);
577 }
578 if (arguments != null && arguments.length > 0) {
579 if (sb.length() != 0) sb.append(", ");
580 sb.append("args: ");
581 sb.append(Arrays.toString(arguments));
582 }
583 if (startTime > 0) {
584 if (sb.length() != 0) sb.append(", ");
585 sb.append("startTime: ");
586 sb.append(startInstant());
587 }
588 if (totalTime != -1) {
589 if (sb.length() != 0) sb.append(", ");
590 sb.append("totalTime: ");
591 sb.append(totalCpuDuration().toString());
592 }
593 sb.append(']');
594 return sb.toString();
595 }
596 }
597 }
|
455 }
456
457 @Override
458 public boolean equals(Object obj) {
459 if (this == obj) {
460 return true;
461 }
462 if (obj instanceof ProcessHandleImpl) {
463 ProcessHandleImpl other = (ProcessHandleImpl) obj;
464 return (pid == other.pid) &&
465 (startTime == other.startTime
466 || startTime == 0
467 || other.startTime == 0);
468 }
469 return false;
470 }
471
472 /**
473 * Implementation of ProcessHandle.Info.
474 * Information snapshot about a process.
475 * The attributes of a process vary by operating system and are not available
476 * in all implementations. Additionally, information about other processes
477 * is limited by the operating system privileges of the process making the request.
478 * If a value is not available, either a {@code null} or {@code -1} is stored.
479 * The accessor methods return {@code null} if the value is not available.
480 */
481 static class Info implements ProcessHandle.Info {
482 static {
483 initIDs();
484 }
485
486 /**
487 * Initialization of JNI fieldIDs.
488 */
489 private static native void initIDs();
490
491 /**
492 * Fill in this Info instance with information about the native process.
493 * If values are not available the native code does not modify the field.
494 * @param pid of the native process
495 */
496 private native void info0(long pid);
497
498 String command;
499 String commandLine;
500 String[] arguments;
501 long startTime;
502 long totalTime;
503 String user;
504
505 Info() {
506 command = null;
507 commandLine = null;
508 arguments = null;
509 startTime = -1L;
510 totalTime = -1L;
511 user = null;
512 }
513
514 /**
515 * Returns the Info object with the fields from the process.
516 * Whatever fields are provided by native are returned.
517 * If the startTime of the process does not match the provided
518 * startTime then an empty Info is returned.
519 *
520 * @param pid the native process identifier
521 * @param startTime the startTime of the process being queried
522 * @return ProcessHandle.Info non-null; individual fields may be null
523 * or -1 if not available.
524 */
525 public static ProcessHandle.Info info(long pid, long startTime) {
526 Info info = new Info();
527 info.info0(pid);
528 if (startTime != info.startTime) {
529 info.command = null;
530 info.arguments = null;
531 info.startTime = -1L;
532 info.totalTime = -1L;
533 info.user = null;
534 }
535 return info;
536 }
537
538 @Override
539 public Optional<String> command() {
540 return Optional.ofNullable(command);
541 }
542
543 @Override
544 public Optional<String> commandLine() {
545 if (command != null && arguments != null) {
546 return Optional.of(command + " " + String.join(" ", arguments));
547 } else {
548 return Optional.ofNullable(commandLine);
549 }
550 }
551
552 @Override
553 public Optional<String[]> arguments() {
554 return Optional.ofNullable(arguments);
555 }
556
557 @Override
558 public Optional<Instant> startInstant() {
559 return (startTime > 0)
560 ? Optional.of(Instant.ofEpochMilli(startTime))
561 : Optional.empty();
562 }
563
564 @Override
565 public Optional<Duration> totalCpuDuration() {
566 return (totalTime != -1)
567 ? Optional.of(Duration.ofNanos(totalTime))
568 : Optional.empty();
569 }
570
571 @Override
572 public Optional<String> user() {
573 return Optional.ofNullable(user);
574 }
575
576 @Override
577 public String toString() {
578 StringBuilder sb = new StringBuilder(60);
579 sb.append('[');
580 if (user != null) {
581 sb.append("user: ");
582 sb.append(user());
583 }
584 if (command != null) {
585 if (sb.length() != 0) sb.append(", ");
586 sb.append("cmd: ");
587 sb.append(command);
588 }
589 if (arguments != null && arguments.length > 0) {
590 if (sb.length() != 0) sb.append(", ");
591 sb.append("args: ");
592 sb.append(Arrays.toString(arguments));
593 }
594 if (commandLine != null) {
595 if (sb.length() != 0) sb.append(", ");
596 sb.append("cmdLine: ");
597 sb.append(commandLine);
598 }
599 if (startTime > 0) {
600 if (sb.length() != 0) sb.append(", ");
601 sb.append("startTime: ");
602 sb.append(startInstant());
603 }
604 if (totalTime != -1) {
605 if (sb.length() != 0) sb.append(", ");
606 sb.append("totalTime: ");
607 sb.append(totalCpuDuration().toString());
608 }
609 sb.append(']');
610 return sb.toString();
611 }
612 }
613 }
|