cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
From: Markus Elfring <Markus.Elfring@web.de>
Cc: Coccinelle <cocci@systeme.lip6.fr>
Subject: Re: [Cocci] Data exchange over network interfaces by SmPL scripts
Date: Sat, 27 Apr 2019 19:24:40 +0200	[thread overview]
Message-ID: <6ec5b70f-39c3-79e5-608f-446a870f02f3@web.de> (raw)
In-Reply-To: <2f138dc8-74cd-3766-cc78-c4bace8579c0@web.de>

[-- Attachment #1: Type: text/plain, Size: 1221 bytes --]

> connecting
>   File "<string>", line 35, in store_statements
> socket.gaierror: [Errno -2] Name or service not known
> Error in Python script, line 56, file …

It seems that the attached adjusted data processing approach can produce
an usable analysis result.

elfring@Sonne:~/Projekte/Coccinelle/janitor> time /usr/bin/python3 list_duplicate_statement_pairs_from_if_branches-server4.py
statement1|statement2|"function name"|"source file"|incidence
dprintk ( "%s: readreg error (reg == 0x%02x, ret == %i)\n" , __func__ , reg , ret ) ;|return - 1 ;|stv0297_readreg|/home/elfring/Projekte/Linux/next-patched/drivers/media/dvb-frontends/stv0297.c|3
dprintk ( "%s: readreg error (reg == 0x%02x, ret == %i)\n" , __func__ , reg1 , ret ) ;|return - 1 ;|stv0297_readregs|/home/elfring/Projekte/Linux/next-patched/drivers/media/dvb-frontends/stv0297.c|3

real	0m1,044s
user	0m0,389s
sys	0m0,055s


Unfortunately, I observed during a few runs on my test system
that the displayed record sets can vary. Thus I guess that this approach
(which works together with Python multi-threading functionality) will need
further software adjustments.
Would you like to add any advices here?

Regards,
Markus

[-- Attachment #2: list_duplicate_statement_pairs_from_if_branches-server4.py --]
[-- Type: text/x-python, Size: 4454 bytes --]

import threading, socket, socketserver, struct, subprocess
inputs = []

def receive_data(s, n):
    d = b''

    while len(d) < n:
        p = s.recv(n - len(d))
        if not p:
           return None

        d += p

    return d

def receive_message(s):
    expect = receive_data(s, 4)
    if not expect:
       return None

    return receive_data(s, struct.unpack(">I", expect)[0])

class threaded_TCP_request_handler(socketserver.BaseRequestHandler):
    def handle(self):
        data = receive_message(self.request)
        if data:
           inputs.append(data.decode())

class threaded_TCP_server(socketserver.ThreadingMixIn, socketserver.TCPServer):
    pass

if __name__ == "__main__":
    server = threaded_TCP_server(("localhost", 1234), threaded_TCP_request_handler)
    with server:
        ip, port = server.server_address
        server_thread = threading.Thread(target = server.serve_forever)
        server_thread.daemon = True
        server_thread.start()
        cp = subprocess.run(["/usr/local/bin/spatch",
                             "--timeout",
                             "9",
                             "--python",
                             "/usr/bin/python3",
                             "-D",
                             "server_id=" + str(ip),
                             "-D",
                             "server_port=" + str(port),
                             "/home/elfring/Projekte/Coccinelle/janitor/list_duplicate_statement_pairs_from_if_branches-client3.cocci",
                             "/home/elfring/Projekte/Linux/next-patched/drivers/media/dvb-frontends/stv0297.c"],
                            capture_output = True, text = True)
        server.shutdown()
        import sys

        if cp.returncode:
           sys.stderr.write("%s\n===\nexit code: %d" % (cp.stderr, cp.returncode))
        else:
           if len(inputs) > 0:
              def report():
                 mapping = {}

                 def insert(x):
                    """Add data to an internal table."""
                    key = x["name"], x["file"], x["line"], x["column"]
                    if key in mapping:
                       sys.stderr.write("""A duplicate key was passed.
function: %s
file: %s
line: %s
column: %d
""" % key)
                       raise RuntimeError
                    else:
                       mapping[key] = x["s1"], x["s2"]

                 def data_import():
                    import json
                    for k in inputs:
                       for v in json.loads(k):
                          insert(v)

                 data_import()
                 from collections import Counter
                 counts = Counter()

                 for k, v in mapping.items():
                    counts[(v[0], v[1], k[0], k[1])] += 1

                 delimiter = "|"
                 duplicates = {}

                 for k, v in counts.items():
                    if v > 1:
                       duplicates[k] = v

                 if len(duplicates.keys()) > 0:
                    sys.stdout.write(delimiter.join(["statement1",
                                                     "statement2",
                                                     '"function name"',
                                                     '"source file"',
                                                     "incidence"])
                                     + "\r\n")

                    for k, v in duplicates.items():
                       sys.stdout.write(delimiter.join([k[0], k[1], k[2], k[3], str(v)])
                                        + "\r\n")
                 else:
                    sys.stderr.write("Duplicate statements were not determined"
                                     " from the following records.\n"
                                     + delimiter.join(["statement1",
                                                       "statement2",
                                                       '"function name"',
                                                       '"source file"'])
                                     + "\r\n")

                    for k, v in counts.items():
                       if v < 2:
                          sys.stderr.write(delimiter.join([k[0], k[1], k[2], k[3]])
                                           + "\r\n")

              report()
           else:
              sys.stderr.write("No result for this analysis!\n")

[-- Attachment #3: list_duplicate_statement_pairs_from_if_branches-client3.cocci --]
[-- Type: text/plain, Size: 1483 bytes --]

@initialize:python@
s_id << virtual.server_id;
s_port << virtual.server_port;
@@
import json, socket, struct, sys

if s_id == False:
   s_id = "localhost"

target = s_id, int(s_port) if s_port else 1234
sys.stderr.write("Using Python version:\n%s\n" % (sys.version))
sys.stderr.write('Connections will be tried with server “%s” on port “%d”.\n'
                 % target)

def store_statements(fun, source, s1, s2):
    """Send data for the service."""
    records = []

    for place in source:
       records.append('{"name":%s,"file":%s,"line":%s,"column":%s,"s1":%s,"s2":%s}'
                      % (json.dumps(fun),
                         json.dumps(place.file),
                         json.dumps(place.line),
                         json.dumps(int(place.column) + 1),
                         json.dumps(s1),
                         json.dumps(s2)))

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as so:
        so.connect(target)
        result = "[\n"
        result += ",\n".join(records)
        result += "\n]"
        b = bytes(result, "utf8")
        p = struct.pack(">I", len(b))
        p += b
        so.sendall(p)

@searching exists@
identifier work;
statement s1, s2;
position pos;
type T;
@@
 T work(...)
 {
 ... when any
 if (...)
 {
 ... when any
 s1@pos
 s2
 }
 ... when any
 }

@script:python collection@
fun << searching.work;
s1 << searching.s1;
s2 << searching.s2;
place << searching.pos;
@@
store_statements(fun, place, s1, s2)

[-- Attachment #4: Type: text/plain, Size: 136 bytes --]

_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

  parent reply	other threads:[~2019-04-27 17:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-20 18:50 [Cocci] Checking import of code search results into a table by parallel SmPL data processing Markus Elfring
     [not found] ` <alpine.DEB.2.21.1904202112150.2499@hadrien>
2019-04-20 19:31   ` Markus Elfring
2019-04-23  9:48   ` Markus Elfring
2019-04-24  6:25 ` [Cocci] Rejecting parallel execution of SmPL scripts Markus Elfring
2019-04-25  8:06 ` [Cocci] Data exchange over network interfaces by " Markus Elfring
     [not found]   ` <alpine.DEB.2.21.1904251039000.2550@hadrien>
2019-04-25 10:32     ` Markus Elfring
2019-04-27 17:20       ` Markus Elfring
2019-04-27 17:24       ` Markus Elfring [this message]
2019-04-30  8:55         ` Markus Elfring
2019-06-01 11:13         ` Markus Elfring
2019-04-25 18:12 ` [Cocci] Data exchange through message queue " Markus Elfring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6ec5b70f-39c3-79e5-608f-446a870f02f3@web.de \
    --to=markus.elfring@web.de \
    --cc=cocci@systeme.lip6.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).