Skip to content

ENT-8096: Implemented policy profiler in cf-agent#5903

Merged
olehermanse merged 1 commit into
cfengine:masterfrom
victormlg:profiler-3
Oct 27, 2025
Merged

ENT-8096: Implemented policy profiler in cf-agent#5903
olehermanse merged 1 commit into
cfengine:masterfrom
victormlg:profiler-3

Conversation

@victormlg

@victormlg victormlg commented Oct 14, 2025

Copy link
Copy Markdown
Contributor

Example policy:

bundle agent main
{
  commands:
    "/bin/sleep 1";
}

To generate the profiling tree, run:
sudo /var/cfengine/bin/cf-agent -Kpf ~/profiler5.cf

{
      "events": [
        {
          "elapsed": 1004137457,
          "filename": "/home/vagrant/profiler5.cf",
          "id": "promise_commands_/home/vagrant/profiler5.cf_67_4",
          "name": "commands",
          "offset": {
            "context": 0,
            "end": 0,
            "line": 4,
            "start": 67
          },
          "type": "promise"
        },
        {
          "elapsed": 1004677573,
          "filename": "/home/vagrant/profiler5.cf",
          "id": "bundle_main_/home/vagrant/profiler5.cf_13_1",
          "name": "main",
          "offset": {
            "context": 0,
            "end": 85,
            "line": 1,
            "start": 13
          },
          "type": "bundle"
        }
      ],
      "policy": {
        "bodies": [],
        "bundles": [
          {
            "arguments": [],
            "bundleType": "agent",
            "line": 1,
            "name": "main",
            "namespace": "default",
            "promiseTypes": [
              {
                "contexts": [
                  {
                    "name": "any",
                    "promises": [
                      {
                        "attributes": [],
                        "line": 4,
                        "promiser": "/bin/sleep 1"
                      }
                    ]
                  }
                ],
                "line": 3,
                "name": "commands"
              }
            ],
            "sourcePath": "/home/vagrant/profiler5.cf"
          }
        ]
      }
    }

@victormlg

victormlg commented Oct 14, 2025

Copy link
Copy Markdown
Contributor Author

I still need to add functions execution in the profiler

edit: this is done

@github-advanced-security github-advanced-security AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

CodeQL found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@victormlg victormlg force-pushed the profiler-3 branch 2 times, most recently from 20debf1 to 7f623aa Compare October 20, 2025 13:22
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
@victormlg victormlg force-pushed the profiler-3 branch 3 times, most recently from 6347574 to 1c3e1ca Compare October 21, 2025 09:21
@victormlg victormlg requested a review from larsewi October 21, 2025 10:09

@larsewi larsewi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some smaller comments

Comment thread libpromises/eval_context.c Outdated
Comment thread libpromises/eval_context.c Outdated
Comment thread libpromises/eval_context.c Outdated
Comment thread libpromises/eval_context.c Outdated
Comment thread libpromises/eval_context.c Outdated
Comment thread libpromises/eval_context.c Outdated
Comment thread libpromises/eval_context.c Outdated
Comment thread libpromises/eval_context.c Outdated
Comment thread libpromises/eval_context.c Outdated
Comment thread libpromises/eval_context.h Outdated
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
@victormlg victormlg force-pushed the profiler-3 branch 2 times, most recently from c988d98 to dd5817d Compare October 24, 2025 11:58
@victormlg victormlg requested a review from larsewi October 24, 2025 12:56
Comment thread libpromises/eval_context.c Outdated
{
assert(ctx != NULL);
assert(last_event != NULL);
SeqAppend(ctx->events, last_event); // keep track of EventFrames for cleanup

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@victormlg I think this part (pushing an event into the Seq) is the only thing we want to do while evaluating policy. The rest (building a nice tree, hashmaps, summarizing, sorting, etc. Should all appen by iterating over the events after policy evaluation is done.

Comment thread libpromises/eval_context.c Outdated
return;
}

JsonElement *tree = EventFrameTreeToJson(ctx->root);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here we should loop over the "unprocessed" events in the Seq, and convert them to the proper tree.

Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Fixed
Comment thread libpromises/eval_context.c Outdated
Comment thread libpromises/eval_context.c Outdated
Comment thread cf-agent/cf-agent.c Outdated
Changelog: The policy profiling logic is now implemented natively within the cf-agent, replacing the previous reliance on cf-agent logs and the cf-profile.pl script for faster execution.
Ticket: ENT-8096
Signed-off-by: Victor Moene <victor.moene@northern.tech>
@olehermanse olehermanse merged commit c8c9c39 into cfengine:master Oct 27, 2025
12 checks passed
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