C Parse Proc Net Dev

The /proc/net/dev file contains statistics about the configured network interfaces. Each line is dedicated to one network interface and it contains statistics for receive and transmit. The statistics include metrics such total number of received/transmittted bytes, packets, drops, errors and so on. Jan 20, 2019 /proc/pid/maps. A file containing the currently mapped memory regions and their access permissions. See mmap(2) for some further information about memory mappings. #memory map region procmapsparser represents a memory region with this C structure. How to get route information fron C/C in linux. Hi, How to get the router/gateway information from a C/C program. Parsing /proc/net/route is fine. The entries in this are updated after adding a new route. But after deleting that it is not reflecting in /proc/net/route file.(still the old data).

This chapter describes how you can use the Pro*C/C++ Precompiler to precompile your C++ embedded SQL application, and how Pro*C/C++ generates C++ compatible code. This chapter contains the following topics:

Understanding C++ Support

To understand how Pro*C/C++ supports C++, you must understand the basic functional capabilities of Pro*C/C++. In particular, you must be aware of how Pro*C/C++ differs from Pro*C Version 1.

The basic capabilities of Pro*C/C++ are:

  • Full C preprocessor support. You can use #define, #include, #ifdef, and other preprocessor directives in your Pro*C/C++ program, to handle constructs that the precompiler itself must process.

  • Use of native C structures as host variables, including the ability to pass structs (or pointers to structs) as host variables to functions, and write functions that return host structures or struct pointers.

To support its C preprocessor capabilities and to enable host variables to be declared outside a special Declare Section, Pro*C/C++ incorporates a complete C parser. The Pro*C/C++ parser is a C parser; it cannot parse C++ code.

This means that for C++ support, you must be able to disable the C parser, or at least partially disable it. To disable the C parser, the Pro*C/C++ Precompiler includes command-line options to give you control over the extent of C parsing that Pro*C/C++ performs on your source code.

See Also:

No Special Macro Processing

Using C++ with Pro*C/C++ does not require any special preprocessing or special macro processors that are external to Pro*C/C++. There is no need to run a macro processor on the output of the precompiler to achieve C++ compatibility.

If you are a user of a release of Pro*C/C++ Precompiler before this one, and you did use macro processors on the precompiler output, you should be able to precompile your C++ applications using Pro*C/C++ with no changes to your code.

Precompiling for C++

To control precompilation so that it accommodates C++, there are four considerations:

  • Code emission by the precompiler

  • Parsing capability

  • The output filename extension

  • The location of system header files

Code Generation

You must be able to specify what kind of code, C compatible code or C++ compatible code, the precompiler generates. Pro*C/C++ by default generates C code. C++ is not a perfect superset of C. Some changes are required in generated code so that it can be compiled by a C++ compiler.

For example, in addition to emitting your application code, the precompiler interposes calls to its runtime library, SQLLIB. The functions in SQLLIB are C functions. There is no special C++ version of SQLLIB. For this reason, if you want to compile the generated code using a C++ compiler, Pro*C/C++ must declare the functions called in SQLLIB as C functions.

For C output, the precompiler would generate a prototype such as

C++ Parse Int From String

But for C++ compatible code, the precompiler must generate

Net

You control the kind of code Pro*C/C++ generates using the precompiler option CODE. There are three values for this option: CPP, KR_C, and ANSI_C. The differences between these options can be illustrated by considering how the declaration of the SQLLIB function sqlora differs among the three values for the CODE option:

When you specify CODE=CPP, the precompiler

  • Generates C++ compilable code.

  • Gives the output file a platform-specific file extension (suffix), such as '.C' or '.cc', rather than the standard '.c' extension. (You can override this by using the CPP_SUFFIX option.)

  • Causes the value of the PARSE option to default to PARTIAL. You can also specify PARSE=NONE. If you specify PARSE=FULL, an error is issued at precompile time.

  • Allows the use of the C++ style // Comments in your code. This style of Commenting is also permitted inside SQL statements and PL/SQL blocks when CODE=CPP.

  • Pro*C/C++ recognizes SQL optimizer hints that begin with //+.

  • Requires that header files generated by OTT (Object Type Translator) must be included inside a declare section.

    See Also:

    'CODE' for information about the KR_C and ANSI_C values for the CODE option.

Parsing Code

You must be able to control the effect of the Pro*C/C++ C parser on your code. You do this by using the PARSE precompiler option, which controls how the precompiler's C parser treats your code.

C Parse Proc Net Dev Code

The values and effects of the PARSE option are:

Table 12-1 Values and Effects of the PARSE Option

ValuesEffects

PARSE=NONE

The value NONE has the following effects:

  • C preprocessor directives are understood only inside a declare section.

  • You must declare all host variables inside a Declare Section.

  • Precompiler release 1.x behavior

PARSE=PARTIAL

The value PARTIAL has the following effects:

  • All preprocessor directives are understood

  • You must declare all host variables inside a Declare Section

This option value is the default if CODE=CPP

PARSE=FULL

The value FULL has the following effects:

  • The precompiler C parser runs on your code.

  • All Preprocessor directives are understood.

  • You can declare host variables at any place that they can be declared legally in C.


This option value is the default if the value of the CODE option is anything other than CPP. It is an error to specify PARSE=FULL when CODE=CPP.

To generate C++ compatible code, the PARSE option must be either NONE or PARTIAL. If PARSE=FULL, the C parser runs, and it does not understand C++ constructs in your code, such as classes.

Output Filename Extension

Most C compilers expect a default extension of '.c' for their input files. Different C++ compilers, however, can expect different filename extensions. The CPP_SUFFIX option provides the ability to specify the filename extension that the precompiler generates. The value of this option is a string, without the quotes or the period. For example, CPP_SUFFIX=cc, or CPP_SUFFIX=C.

C Parse Proc Net Device

System Header Files

Pro*C/C++ searches for standard system header files, such as stdio.h, in standard locations that are platform specific. Pro*C/C++ does not search for header files with extensions such as hpp or h++. For example, on almost all UNIX systems, the file stdio.h has the full path name /usr/include/stdio.h.

But a C++ compiler has its own version of stdio.h that is not in the standard system location. When you are precompiling for C++, you must use the SYS_INCLUDE precompiler option to specify the directory paths that Pro*C/C++ searches to look for system header files. For example:

Use the INCLUDE precompiler option to specify the location of non-system header files. The directories specified by the SYS_INCLUDE option are searched before directories specified by the INCLUDE option. See also: 'INCLUDE'.

If PARSE=NONE, the values specified in SYS_INCLUDE and INCLUDE for system files are not relevant, since there is no need for Pro*C/C++ to include system header files. (You can, of course, still include Pro*C/C++-specific headers, such sqlca.h, using the EXEC SQL INCLUDE statement.)

Example Programs

This section includes three example Pro*C/C++ programs that include C++ constructs. Each of these programs is available on-line, in your demo directory.

cppdemo2.pc

The next application is a simple modular example. First, execute the following SQL script, cppdemo2.sql, in SQL*Plus:

The header file empclass.h defines the class emp:

The code in empclass.pc contains the emp methods:

C Parse Proc Net Dev Update

The main program, cppdemo2.pc, uses the cursor variable:

read /proc/net/tcp and parse and make statistics
setup.py
fromdistutils.coreimportsetup
setup(name='tcptuneinfo',
version='0.1',
description=',
author='Kazushige TAKEUCHI',
url='http://d.hatena.ne.jp/graceful_life/',
requires=[
'netaddr',
],
# packages=['distutils', 'distutils.command'],
scripts=['tcptune.py'],
)
tcptuneinfo.py
# -*- coding: utf-8 -*-
__author__='Kazushige TAKEUCHI'
frommultiprocessingimportProcess,Pool
fromnetaddrimport*
importdaemon
importtime
fromexceptionsimportKeyboardInterrupt
importsys
network_type= {
IPNetwork('127.0.0.1'): 'localhost',
IPNetwork('172.16.0.0/12'):'management',
}
classTcpConnectionEntry(object):
def__init__(self,entry):
self.local=entry[1]
self.localadr=None
self.remote=entry[2] # addr:port
self.remoteadr=None
self.state=entry[3] # int
queue=entry[4].split(':') # tx:rx
self.queue_tx=int(queue[0],16) # tx:rx
self.queue_rx=int(queue[1],16) # tx:rx
self.user=entry[7]
self.nw_type='unknown'
pass
defdecode_statistics(self):
addr,port=self.remote.split(':')
#print addr
a='%d.%d.%d.%d'% (int(addr[6:8],16),int(addr[4:6],16),int(addr[2:4],16),int(addr[0:2],16)),
self.remoteadr=IPAddress(a[0])
ifself.remoteadr.value0:
self.nw_type='listen'
ifself.nw_type'unknown':
forkeyinnetwork_type.keys():
ifkey.first<=self.remoteadr.valueandself.remoteadr.value<=key.last:
self.nw_type=network_type[key]
pass
ifself.nw_type'unknown':
self.nw_type='default'
pass
returnself
pass
def__str__(self):
return'<Connection: %s %s>'% (self.nw_type, self.remoteadr)
pass
pass
classTcpStatistics(object):
def__init__(self):
importre
metcher='
self.user= {}
self.stat={}
pass
defparse_from_procentry(self,entry):
ent=TcpConnectionEntry(entry)
e=ent.decode_statistics()
ifnotself.stat.has_key(e.nw_type):
self.stat.update(
{e.nw_type: {
'count': 1,
'tx': e.queue_tx,
'rx': e.queue_rx
}
}
)
pass
else:
self.stat[e.nw_type]['count']=self.stat[e.nw_type]['count']+1
self.stat[e.nw_type]['tx']=self.stat[e.nw_type]['tx']+e.queue_tx
self.stat[e.nw_type]['rx']=self.stat[e.nw_type]['rx']+e.queue_rx
pass
# print e
pass
defclear(self):
self.counter=[0,0,0,0,0,0,0,0,0,0,0]
pass
classTcpTuneInfoReader(Process):
def__init__(self,queue=[]):
super(TcpTuneInfoReader, self).__init__()
self.queue=queue
self.tcpinfo_path='/proc/net/tcp'
self.counter=[0,0,0,0,0,0,0,0,0,0,0]
self.tx=0
self.rx=0
self.stat=TcpStatistics()
pass
def_parse_queue(self):
fp=open(self.tcpinfo_path , 'r')
flag=True
forlineinfp:
ifflag:
flag=False
else:
entry=line.split()
idx=int(entry[3],16)
self.counter[idx] =self.counter[idx] +1
self.stat.parse_from_procentry(entry)
pass
fp.close()
fromdatetimeimportdatetime
printdatetime.now(),
printself.stat.stat
self.stat.stat={}
time.sleep(0.02)
pass
pass
defrun(self):
whileTrue:
self._parse_queue()
pass
classTcpTuneInfoDaemon(object):
def__init__(self):
pass
defstart(self):
p=TcpTuneInfoReader()
p.daemon=True
p.start()
self._loop()
pass
def_loop(self):
try:
whileTrue:
time.sleep(1)
pass
exceptKeyboardInterrupt:
printsys.exc_info()[0]
finally:
pass
exit(0)
pass
pass
defmain(argv):
tcptuneinfod=TcpTuneInfoDaemon()
tcptuneinfod.start()
pass
if__name__'__main__':
main(sys.argv)
pass
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment