Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ps command to sdb. #274

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tony-zfs
Copy link

@tony-zfs tony-zfs commented Apr 6, 2021

Pull request checklist

Please check if your PR fulfills the following requirements:

  • [X ] Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • [X ] Build was run locally and any changes were pushed
  • [X ] Lint has passed locally and any fixes were made for failures

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • [ X] Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

Issue Number: 248
#248

What is the new behavior?

  • Supports limited version of ps

Does this introduce a breaking change?

  • Yes
  • [X ] No

Other information

Copy link
Contributor

@sdimitro sdimitro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @tony-zfs thank you for opening the PR!

From what I see the failures in the test-suite are from our frozed version of mypy and the use of -h option from argparse. I have some more details on both bundled together with the rest of my feedback.

from sdb.commands.stacks import Stacks


def _cmdline(obj: drgn.Object) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used anywhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed.

return ""


class Ps(sdb.Locator, sdb.PrettyPrinter):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of code in this class overlaps with threads.py. Instead of duplicating code in both places did you try the following:
[1] Extend the threads command to handle the extra columns that you added (e.g. uid) and also the column logic (e.g -o , -h, etc..)
[2] Make the ps command use the threads command under the hood and keep the arguments that make it look like the linux userland ps command (e.g. -p, --ppid, etc..).

If you want an example of layering a command on top of another command checkout the spa command. It is using the avl walker for its Locator/no-input method, and vdev for its pretty-printer.

As for generic use of the Table class and how to implement the sort-by-column functionality you can look at either the slubs or spl_kmem_caches.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Thanks for the advice.

help="Print only the process IDs of a command")
parser.add_argument('-x', '--x', action='store_true', \
help="Show PID, TIME, CMD")
parser.add_argument('-h', action='store_true', \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately argparse doesn't allow us to use -h for anything other than the help message. Fortunately for us looking at man ps I see that that to skip the headers only the long form argument exists --no-headers and --no-heading.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

input_type = "struct task_struct *"
output_type = "struct task_struct *"

FIELDS: Dict[str, Callable[[drgn.Object], Union[str, int]]] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that it matters if we go ahead with layering the ps command on top of threads but I don't see task mentioned here. What if a user wants to specify it with the -o option?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

help="Show the output without headers.")
parser.add_argument('-p', '--p', type=str, \
help="Select by process ID. Identical to --pid.")
parser.add_argument('-ppid', '--ppid', type=str, \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the long-form and the short-form argument look the same doesn't look right. Looking at man ps it seems like only the long-form is supported --ppid. If you really want a short-form I don't think -P is taken, so we could use that.

In addition, like -p/--pid the type of argument can be a list of integers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

help="Show PID, TIME, CMD")
parser.add_argument('-h', action='store_true', \
help="Show the output without headers.")
parser.add_argument('-p', '--p', type=str, \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at man ps I think the correct long-form for this is --pid.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines 158 to 159
pids = [int(x) for x in self.args.p.split(',')] if self.args.p else []
ppids = [int(x) for x in self.args.ppid.split(',')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens here when we get faulty input? (e.g. "asdf, asdfasdf")

If the result is that we blow up maybe it may be easier to switch from the ps --pid 1,2,3 to ps --pid 1 2 3 notation (both are supported from the userland ps utility in Linux from what I can tell). This would mean that we'd need to change the argument type to something like type=int, nargs="+",.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch - I'll update.

Comment on lines +93 to +99
"ps",
"ps -C bash",
"ps -p 4275",
"ps -e",
"ps -x",
"ps -o pid,ppid",
"ps -A",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for extending the test-suite!

@@ -152,7 +152,7 @@ def __init__(self,
self.match_state = ""

@classmethod
def _init_parser(cls, name: str) -> argparse.ArgumentParser:
def _init_parser(cls, name: str) -> argparse.ArgumentParser():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pressumably mypy bothered you about this change. We've found it to be extremely inconsistent with false-positives being introduced at every new release thus we've frozed our version of mypy in our Github Actions to 0.730 - https://github.com/delphix/sdb/blob/master/.github/workflows/main.yml#L100 . You may want to do the same in your python environment for sdb locally.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - it sure did!

@sebroy
Copy link

sebroy commented Apr 6, 2021

I would love to see sample output of the new subcommand as part of the PR.

@tony-zfs tony-zfs force-pushed the add_ps_support branch 2 times, most recently from f6d2531 to 27dac29 Compare April 13, 2021 16:20
@tony-zfs
Copy link
Author

Updated. Is there anything else needed to get this to be accepted?

@PaulZ-98
Copy link
Contributor

I would love to see sample output of the new subcommand as part of the PR.

If you drill into the commit, there are sample outputs used in the automated testing of the new command. Look for tests/integration/data/regression_output/linux/ps -e and the like.

@dlpx-tfc-github-manager dlpx-tfc-github-manager bot deleted the branch delphix:master January 5, 2023 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants