I'm using the C interface for XPC services; incidentally my XPC service runs very nicely asides from the following problem.
The other day I tried to send a "large" array via XPC; of the order of 200,000 entries. Usually, my application deals with data of the order of a couple of thousand entries and has no problems with that. For other uses an array of this size may not be special.
Here is my C++ server code for generating the array:
xpc_connection_t remote = xpc_dictionary_get_remote_connection(event);
xpc_object_t reply = xpc_dictionary_create_reply(event);
xpc_object_t times;
times = xpc_array_create(NULL, 0);
for(unsigned int s = 0; s < data.size(); s++)
{
xpc_object_t index = xpc_uint64_create(data[s]);
xpc_array_append_value(times, index);
}
xpc_dictionary_set_value(reply, "times", times);
xpc_connection_send_message(remote, reply);
xpc_release(times);
xpc_release(reply);
and here is the client code:
xpc_object_t times = xpc_dictionary_get_value(reply, "times");
size_t count = xpc_array_get_count(times);
for(int c = 0; c < count; c++)
{
long my_time = xpc_array_get_uint64(times, c);
local_times.push_back(my_time);
}
If I try to handle a large array I get a seg fault (SIGSEGV)
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libxpc.dylib 0x00007fff90e5cc02 xpc_array_get_count + 0
When you say "extremely big array" are you speaking of something that launchd might regard as a resource-hog and kill?
XPC is only really meant for short-fast transactional runs rather than long-winded service-based runs.
If you're going to make calls that make launchd wait, then I'd suggest you try https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html
When the Service dies.. Are any specific events other then SIG_ABORTS etc... fired?
Do you get "xpc service was invalidated" (which usually means launchD killed it, or did you get "xpc service/exited prematurely" which usually is handler code error.
Related
Well, I certainly should go to python since I did several functions of this type, keyboard event and mouse event, but decide to try to learn the windows api.
My goal is to know when button 1 of the mouse is pressed.
I created this file in a very beginner way, it returns in mouseData only 0.
The curious thing is that whenever I run it, it flashes my monitor at short intervals in blinks, but between 1 second with it off. Very strange that, execution is not viable.
Could someone help me understand and try to execute to see if it is only here.
Code:
int main()
{
DWORD mouseData = 0;
MOUSEINPUT tagMouse;
tagMouse.dx = 0;
tagMouse.dy = 0;
tagMouse.mouseData = mouseData;
tagMouse.dwFlags = MOUSEEVENTF_XDOWN;
tagMouse.dwExtraInfo = 0;
INPUT tagInput;
tagInput.type = INPUT_MOUSE;
tagInput.mi = tagMouse;
while (true) {
if (GetAsyncKeyState(VK_DELETE)) break;
SendInput(1, &tagInput, sizeof(INPUT));
printf("KEYWORD: %d\n", mouseData);
Sleep(500);
}
system("pause");
return 0;
}
I can reproduce your reported 'symptoms' - and the effect is really brutal!
Now, while I cannot offer a full explanation, I can offer a fix! You have an uninitialized field in your tagMouse structure (the time member, which is a time-stamp used by the system). Setting this to zero (which tells the system to generate its own time-stamp) fixes the problem. So, just add this line to your other initializer statements:
//...
tagMouse.dwExtraInfo = 0;
tagMouse.time = 0; // Adding this line fixes it!
//...
Note: I, too, would appreciate a fuller explanation; however, an uninitialized field, to me, smells like undefined behaviour! I have tried a variety of other values (i.e. not zero) for the time field but haven't yet found one that works.
The discussion here on devblogs may help. This quote seems relevant:
And who knows what sort of havoc that will create if a program checks
the timestamps and notices that they are either from the future or
have traveled back in time.
"select * from tables" query in MySQL connector/libmysql C is very slow in getting the results:
Here is my code in C :
int getfrommysql() {
time_t starttime, endtime;
time(&starttime);
double st;
st = GetTickCount();
MYSQL *sqlconn = NULL;
MYSQL_RES * res = NULL;
MYSQL_ROW row = NULL;
MYSQL_FIELD * field;
/*char ipaddr[16];
memset(ipaddr,0,sizeof(ipaddr));*/
char * sqlquery = "select * from seat_getvalue";
sqlconn = malloc(sizeof(MYSQL));
sqlconn = mysql_init(sqlconn);
mysql_real_connect(sqlconn, "111.111.111.111", "root", "password", "database", 0, NULL, 0);
char query[100];
memset(query, 0, 100);
strcpy(query, "select * from seat_getvalue");
mysql_query(sqlconn, query);
res = mysql_store_result(sqlconn);
int col_num, row_num;
if (res) {
col_num = res->field_count;
row_num = res->row_count;
printf("\nthere is a %d row,%d field table", res->row_count, res->field_count);
}
for (int i = 0; i < row_num; i++) {
row = mysql_fetch_row(res);
for (int j = 0; j < col_num; j++) {
printf("%s\t", row[j]);
}
printf("\n");
}
mysql_close(sqlconn);
time(&endtime);
double et = GetTickCount();
printf("the process cost time(get by GetTickCount):%f",et-st);
printf("\nthere is a %d row,%d field table", res->row_count, res->field_count);
}
Apart from the fact, that there isn't even a question given in your post, you are comparing apples to oranges. Mysql gives you (I think - correct me if I am wrong) the time needed to execute the query, while in your C code you measure the time that passed between the start and the end of the program. This is wrong for at least two reasons:
Difference between two GetTickCount() calls gives you the time that has passed between the calls in the whole system, not time spent executing your software. These are two different things, because your process does not have to executed from the beginning to the end uninterrupted - it can (and probably will be) swapped for another process in the middle of the execution, it can be interrupted etc. The whole time the system spent doing stuff outside your program will be added to your measurements. To get time spent on the execution of your code you could probably use GetProcessTimes or QueryProcessCycleTime.
Even if you did use an appropriate method of retrieving your time, you are timing the wrong part of the code. Instead of measuring the time spent just on executing the query and retrieving the results, you measure the whole execution time: estabilishing connection, copying the query, executing it, storing the results, fetching them, printing them and closing the connection. That's quite different from what mysql measures. And printing hundreds of lines can take quite a lot of time, depending on your shell - more than the actual SQL query execution. If you want to know how much time the connector needs to retrieve the data, you should benchmark only the code responsible for executing the query and data retrieval. Or, even better, use some dedicated performance monitoring tools or libraries. I can't point a specific solution, because I never performed tests like that, but there certainly must be some.
I have an AWS Lambda written in NodeJS the invocation process is pretty straightforward
NODEJS -> NodeModule(CPP) -> Extern C Function, this setup is compiled with node-gyp.
You can see the entire code here at https://drive.google.com/open?id=0B-2d-CuY5fkwS3lwdE96R1V6NEk
The CPP node module invokes a function in C, that runs a loop. and increments two variables one in the scope of the C function and the other in main scope of the C code.
When you run this code locally. The loop increments and both the variables reach 11, as expected how much ever you run it. But when you run this same code in AWS Lambda, there is some sort of "memory" for each invocation. And the variable in the general scope, which is not getting reset is increasing, in multiples of 11, 22, 33 etc.
To repeat, this never happens locally, both the variables are always at 11.
you can build by running
1. node-gyp clean configure build
2. node app.js ( for local running)
Index.js is for AWS Lambda
I really cant explain this behavior? is there some sort of context or some sort of "memory" or caching that is available with Lambda?
I have made an Open API gateway for the same. (Feel free to refresh and see the "memory" in action).
https://koj2yva6z9.execute-api.us-east-1.amazonaws.com/dev/testLambdaCache
This behavior is sometimes inconsistent, sometimes the count resets. Or you can reset by uploading new AWS lambda code.
Any thoughts on this strange behavior is appreciated.
app.js (used for local testing)
var addon = require('./build/Release/addon');
console.log(addon.testCache());
console.log(" addon method completed");
index.js (used in lambda)
console.log('Loading function');
exports.handler = (event, context, callback) => {
var addon = require('./build/Release/addon');
var returnvalue=addon.testCache();
console.log(returnvalue);
console.log(" addon method completed");
callback(null, "success::"+returnvalue);
}
base.cc (wrapper for C code)
#include <node.h>
#include <iostream>
#include <stdlib.h>
#include<string>
#include<cstring>
using namespace std;
extern "C" char* testCache();
namespace demo {
using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
using v8::Exception;
void Method(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
cout << "C++ method started\n";
char *returnStrings=NULL;
returnStrings= testCache();
args.GetReturnValue().Set(String::NewFromUtf8(isolate, returnStrings ));
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "testCache", Method);
}
NODE_MODULE(addon, init)
}
decoder.c (c code running the loop)
int tmpCounter=0;
char* testCache()
{
int counter=0;
printf("Local counter --> %d Global Counter --> %d\n",counter,tmpCounter);
for(int i=0;i <10; i++)
{
counter = counter +1;
tmpCounter = tmpCounter +1;
//sleep(1);
}
printf("Local counter --> %d Global Counter --> %d\n",counter,tmpCounter);
counter=counter+1;
tmpCounter=tmpCounter+1;
char strCounter[100];
char strTmpCounter[100];
snprintf(strCounter, 16, "%d", counter);
snprintf(strTmpCounter, 16, "%d", tmpCounter);
char *returnString=NULL;
returnString=malloc(1000);
strcat(returnString, "Count:");
strcat(returnString, strCounter);
strcat(returnString, " TmpCount:");
strcat(returnString, strTmpCounter);
strcat(returnString, "\0");
printf("%s\n",returnString);
fflush(stdout);
return returnString;
}
is there some sort of context or some sort of "memory" or caching that is available with Lambda?
I wouldn't say it's "available," in the sense that it is predictable or consistent, because you should not design around it, but yes, there is container reuse.
To see this in action:
Create a uuid, or random number, or something like that, and store it in a global variable outside your handler. Then, inside the handler, log it. You'll see that the same process or group of processes (as identified by the uuid) will likely, but not necessarily, handle subsequent requests that are close together in time.
Let’s say your function finishes, and some time passes, then you call it again. Lambda may create a new container all over again [...]
However, if you haven’t changed the code and not too much time has gone by, Lambda may reuse the previous container. This offers some performance advantages to both parties: Lambda gets to skip the nodejs language initialization, and you get to skip initialization in your code. Files that you wrote to /tmp last time around will still be there if the sandbox gets reused.
https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/
All call to strcat into testCache are UB because of mallocated memory is not inited.
Change it using calloc
returnString=calloc(1, 1000);
or change the first one with sprintf
sprintf(returnString, "Count:");
Moreover strcat(returnString, "\0"); is useless
Finally always check malloc & friend returned value, e.g.
if (returnString != NULL)
{
// OK, MEMORY ALLOCATED
}
else
{
// ERROR
}
I am relatively new to working with threads in Win32 api and have reached a problem that i am unable to work out.
Heres my problem, i have 4 threads (they work as intended) that allow the operator to test 4 terminals. In each thread i am trying to send a message to the main windows form with either Pass or Fail, this is placed within a listbox. Below is one of the threads, the remaining are exactly the same.
void Thread1(PVOID pvoid)
{
for(int i=0;i<numberOfTests1;i++) {
int ret;
double TimeOut = 60.0;
int Lng = 1;
test1[i].testNumber = getTestNumber(test1[i].testName);
unsigned char Param[255] = {0};
unsigned char Port1 = port1;
ret = PSB30_Open(Port1, 16);
ret = PSB30_SendOrder (Port1, test1[i].testNumber, &Param[0], &Lng, &TimeOut);
ret = PSB30_Close (Port1);
if(*Param == 1) SendDlgItemMessage(hWnd,IDT_RESULTLIST1,LB_ADDSTRING,i,(LPARAM)"PASS");
else SendDlgItemMessage(hWnd,IDT_RESULTLIST1,LB_ADDSTRING,i,(LPARAM)"FAIL");
}
_endthread();
}
I have debugged the code and it does everything except populate the listbox, i assume because its a thread i am missing something as the same code works outwith the thread. Do i need to put the thread to sleep while it sends the message to the main window?
Any help is appreciated.
Cheers
You don't want your secondary threads trying to manipulate your UI elements directly (such as the SendDlgItemMessage). Instead, you normally want to post something like a WM_COMMAND or WM_USER+N to the main window, and let that manipulate the UI elements accordingly.
I am migrating some code of CYBOI from Xlib to XCB.
CYBOI uses a couple of threads for different communication channels like:
serial_port, terminal, socket, x_window_system.
However, it uses these threads only for signal/event/data detection;
the actual receiving and sending is done in the main thread,
in order to avoid any multi-threading conflicts of address space.
For the x_window_system channel, I previously detected events in a thread:
int n = XEventsQueued(display, QueuedAfterReading);
Upon detection of an event, an "interrupt flag" was set.
Afterwards, the main thread was reading the actual event using:
XNextEvent(display, &event);
When no more events were available, the main thread stopped receiving events
and the x_window_system channel thread started listening with XEventsQueued again.
Now, I am migrating the code to X C Binding (XCB).
There is a blocking function "xcb_wait_for_event" which is fine for reading an event.
What I miss is some function "peeking ahead" if there are events pending,
WITHOUT actually returning/removing the event from the queue.
I was reading the web for a couple of hours now, but am not able to find such a function.
The "xcb_poll_for_event" does not help. Blocking is fine for me,
since my event detection runs in its own thread.
The "xcb_request_check" as third input function does not seem to be what I want.
Could somebody help me out?
Thanks,
Christian
Are you looking for xcb_poll_for_queued_event(xcb_connection_t *c) which returns the next event without reading from the connection?
First, thanks to Julien for his reply.
I have studied the XCB 1.9 sources and found out that the
"xcb_poll_for_queued_event" function is not what I need.
The functions "xcb_poll_for_event" and "xcb_poll_for_queued_event"
both call "poll_for_next_event".
The functions "poll_for_next_event" and "xcb_wait_for_event"
both call "get_event".
If "get_event" finds an event, it changes the internal
linked list to point to the next event. However, I would
prefer NOT to change the event queue AT ALL, independent
from whether or not events are available.
I therefore propose to add a function like the following to XCB:
void* NULL_POINTER = (void*) 0;
int xcb_test_for_event(xcb_connection_t* c) {
int r = 0;
if (c != NULL_POINTER) {
struct _xcb_in in = c->in;
struct event_list* l = in.events;
if (l != NULL_POINTER) {
xcb_generic_event_t* e = l->event;
if (e != NULL_POINTER) {
r = 1;
}
}
}
return r;
}
This would allow me to write an endless loop like:
while (!xcb_test_for_event(connection)) {
sleep(t);
}
This is comparable to the old Xlib function:
int n = XEventsQueued(d, QueuedAfterReading);
which just checked the number of events in the event queue.
The "XEventsQueued" function always returns immediately WITHOUT
input/output, if there are events already in the queue.
Thanks
Christian